简体   繁体   English

如何减少对象数组以对其进行排序并删除不需要的嵌套?

[英]How to reduce the array of objects to order it and remove unwanted nesting?

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 > Station > TourStation > Station > TourStation > Station 的形式出现

TourStation stores info about the connection and nested stations, Station contains info about the station and its children TourStation 存储有关连接和嵌套站的信息,Station 包含有关该站及其子站的信息

But I need it in a form of Station > Station > Station (Station with station children, 2 levels deep)但我需要以 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这是针对此问题的 codesandbox: 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.我怎样才能使用 reduce function 或其他东西来让它工作。 If there was no nesting, a simple DATA.map(item => item.station) would work.如果没有嵌套,一个简单的 DATA.map(item => item.station) 就可以了。

 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.sort((a,b)=>a.order-b.order).map(({station:{children=[], ...rest}})=> ({...rest, children:f(children)})) const result = f(data) console.log(result)

You just had to run map recursively:你只需要递归地运行map

 const data = [{ 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 } }] } } ] } }, { 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: [] } } ]; const leaveOnlyStations = (tourStations) => tourStations.sort((a, b) => a.order - b.order).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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM