Home > OS >  How to reduce the array of objects to order it and remove unwanted nesting?
How to reduce the array of objects to order it and remove unwanted nesting?

Time:01-20

I'm having some problems with returning a set of data.

The data comes in a form of TourStation > Station > TourStation > Station > TourStation > Station

TourStation stores info about the connection and nested stations, Station contains info about the station and its children

But I need it in a form of Station > Station > Station (Station with station children, 2 levels deep)

Here is the data in question:

//Data structure
const DATA = [
  {
    id: 47,
    tourId: 37,
    stationId: 7,
    parentStationId: null,
    order: 0,
    station: {
      id: 7,
      textCode: "1234",
      published: true,
      subjectId: 2,
      created: "2022-07-12T19:01:17.049Z",
      updated: "2022-07-15T11:36:46.195Z",
      expired: null,
      children: []
    }
  },
  {
    id: 50,
    tourId: 37,
    stationId: 9,
    parentStationId: null,
    order: 1,
    station: {
      id: 9,
      textCode: "asdd",
      published: true,
      subjectId: 2,
      created: "2022-07-15T13:47:55.557Z",
      updated: "2022-08-10T14:32:35.528Z",
      expired: null,
      children: [
        {
          id: 51,
          tourId: 37,
          stationId: 10,
          parentStationId: 9,
          order: 0,
          station: {
            id: 10,
            textCode: "123",
            published: true,
            subjectId: 2,
            created: "2022-07-25T11:49:21.688Z",
            updated: "2022-07-25T11:50:25.445Z",
            expired: null,
            children: []
          }
        },
        {
          id: 48,
          tourId: 37,
          stationId: 11,
          parentStationId: 9,
          order: 1,
          station: {
            id: 11,
            textCode: "sada",
            published: true,
            subjectId: 2,
            created: "2022-07-25T11:50:46.021Z",
            updated: "2022-07-25T11:50:48.567Z",
            expired: null,
            children: [
              {
                id: 49,
                tourId: 37,
                stationId: 12,
                parentStationId: 11,
                order: 0,
                station: {
                  id: 12,
                  textCode: "ASD",
                  published: true,
                  subjectId: 2,
                  created: "2022-08-10T11:07:38.790Z",
                  updated: "2023-01-20T12:44:59.925Z",
                  expired: null
                }
              }
            ]
          }
        }
      ]
    }
  }
];

And this is the expected result after reducing it

// expected result, ordered by "order" variable from above
const dataExpected = [
  {
    id: 7,
    textCode: "1234",
    published: true,
    subjectId: 2,
    created: "2022-07-12T19:01:17.049Z",
    updated: "2022-07-15T11:36:46.195Z",
    expired: null,
    children: []
  },
  {
    id: 9,
    textCode: "asdd",
    published: true,
    subjectId: 2,
    created: "2022-07-15T13:47:55.557Z",
    updated: "2022-08-10T14:32:35.528Z",
    expired: null,
    children: [
      {
        id: 10,
        textCode: "123",
        published: true,
        subjectId: 2,
        created: "2022-07-25T11:49:21.688Z",
        updated: "2022-07-25T11:50:25.445Z",
        expired: null,
        children: []
      },
      {
        id: 11,
        textCode: "sada",
        published: true,
        subjectId: 2,
        created: "2022-07-25T11:50:46.021Z",
        updated: "2022-07-25T11:50:48.567Z",
        expired: null,
        children: [
          {
            id: 12,
            textCode: "ASD",
            published: true,
            subjectId: 2,
            created: "2022-08-10T11:07:38.790Z",
            updated: "2023-01-20T12:44:59.925Z",
            expired: null
          }
        ]
      }
    ]
  }
];

const dataToReturn = DATA.reduce((prev, curr, index, arr) => {
  //Only get the station and its children, and order them by "order"
}, []);

console.log("RETURNED: ", JSON.stringify(dataToReturn, null, 2));
console.log("EXPECTED: ", JSON.stringify(dataExpected, null, 2));

Here is a codesandbox for this issue: https://codesandbox.io/s/sweet-forest-h23gsi?file=/src/index.js:300-3978

How can I use the reduce function, or something else to get this to work. If there was no nesting, a simple DATA.map(item => item.station) would work.

CodePudding user response:

const data = [{"id":47,"tourId":37,"stationId":7,"parentStationId":null,"order":0,"station":{"id":7,"textCode":"1234","published":true,"subjectId":2,"created":"2022-07-12T19:01:17.049Z","updated":"2022-07-15T11:36:46.195Z","expired":null,"children":[]}},{"id":50,"tourId":37,"stationId":9,"parentStationId":null,"order":1,"station":{"id":9,"textCode":"asdd","published":true,"subjectId":2,"created":"2022-07-15T13:47:55.557Z","updated":"2022-08-10T14:32:35.528Z","expired":null,"children":[{"id":51,"tourId":37,"stationId":10,"parentStationId":9,"order":0,"station":{"id":10,"textCode":"123","published":true,"subjectId":2,"created":"2022-07-25T11:49:21.688Z","updated":"2022-07-25T11:50:25.445Z","expired":null,"children":[]}},{"id":48,"tourId":37,"stationId":11,"parentStationId":9,"order":1,"station":{"id":11,"textCode":"sada","published":true,"subjectId":2,"created":"2022-07-25T11:50:46.021Z","updated":"2022-07-25T11:50:48.567Z","expired":null,"children":[{"id":49,"tourId":37,"stationId":12,"parentStationId":11,"order":0,"station":{"id":12,"textCode":"ASD","published":true,"subjectId":2,"created":"2022-08-10T11:07:38.790Z","updated":"2023-01-20T12:44:59.925Z","expired":null}}]}}]}}]

const f = data => 
  data.map(({station:{children=[], ...rest}})=>
    ({...rest, children:f(children)})
  )

const result = f(data)
console.log(result)

CodePudding user response:

You just had to run map recursively:

const data = [{
    id: 47,
    tourId: 37,
    stationId: 7,
    parentStationId: null,
    order: 0,
    station: {
      id: 7,
      textCode: "1234",
      published: true,
      subjectId: 2,
      created: "2022-07-12T19:01:17.049Z",
      updated: "2022-07-15T11:36:46.195Z",
      expired: null,
      children: []
    }
  },
  {
    id: 50,
    tourId: 37,
    stationId: 9,
    parentStationId: null,
    order: 1,
    station: {
      id: 9,
      textCode: "asdd",
      published: true,
      subjectId: 2,
      created: "2022-07-15T13:47:55.557Z",
      updated: "2022-08-10T14:32:35.528Z",
      expired: null,
      children: [{
          id: 51,
          tourId: 37,
          stationId: 10,
          parentStationId: 9,
          order: 0,
          station: {
            id: 10,
            textCode: "123",
            published: true,
            subjectId: 2,
            created: "2022-07-25T11:49:21.688Z",
            updated: "2022-07-25T11:50:25.445Z",
            expired: null,
            children: []
          }
        },
        {
          id: 48,
          tourId: 37,
          stationId: 11,
          parentStationId: 9,
          order: 1,
          station: {
            id: 11,
            textCode: "sada",
            published: true,
            subjectId: 2,
            created: "2022-07-25T11:50:46.021Z",
            updated: "2022-07-25T11:50:48.567Z",
            expired: null,
            children: [{
              id: 49,
              tourId: 37,
              stationId: 12,
              parentStationId: 11,
              order: 0,
              station: {
                id: 12,
                textCode: "ASD",
                published: true,
                subjectId: 2,
                created: "2022-08-10T11:07:38.790Z",
                updated: "2023-01-20T12:44:59.925Z",
                expired: null
              }
            }]
          }
        }
      ]
    }
  }
];

const leaveOnlyStations = (tourStations) => tourStations.map(tourStation => {
  const station = tourStation.station;
  const subTourStations = station.children ?? [];

  if (subTourStations.length === 0) return station;

  station.children = leaveOnlyStations(subTourStations);

  return station;
})

console.log(leaveOnlyStations(data));

  • Related