如何减少对象数组的排序和删除不必要的嵌套?



我在返回一组数据时遇到了一些问题。

数据以TourStation>站在TourStation祝辞站在TourStation祝辞站

TourStation存储有关连接和嵌套站点的信息,Station包含有关站点及其子站点的信息

但是我需要以Station>站在车站(有车站儿童的车站,2层深)

下面是所讨论的数据:

//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
}
}
]
}
}
]
}
}
];

而这是化简后的预期结果

// 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));

这里是这个问题的代码盒:https://codesandbox.io/s/sweet-forest-h23gsi?file=/src/index.js:300-3978

我如何使用减少函数,或其他东西来让这个工作。如果没有嵌套,则使用简单的DATA。地图(项=比;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)

你只需要递归地运行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));

相关内容

  • 没有找到相关文章

最新更新