如何在某个 属性 更改后向数组添加新条目?
How to add a new entry to an array after a certain property changes?
我正在尝试在某个 属性 更改后向数组添加新条目,在我的例子中是“位置”更改。但是,我很难找到合适的方法,因为操作取决于数组中的下一个对象。
const initalData = [
{ storyID: 1, place: 12, type: "Story" },
{ storyID: 5, place: 12, type: "Story" },
{ storyID: 99, place: 45, type: "Story" },
{ storyID: 8, place: 31, type: "Story" },
{ storyID: 16, place: 31, type: "Story" },
{ storyID: 20, place: 45, type: "Story" },
{ storyID: 22, place: 45, type: "Story" },
];
const targetData = [
{ placeID: 12, type: "Place" }, // new added entry based on the next "place"
{ storyID: 1, place: 12, type: "Story" },
{ storyID: 5, place: 12, type: "Story" },
{ placeID: 45, type: "Place" }, // // new added entry based on the next "place"
{ storyID: 99, place: 45, type: "Story" },
{ placeID: 31, type: "Place" }, // new added entry based on the next "place"
{ storyID: 8, place: 31, type: "Story" },
{ storyID: 16, place: 31, type: "Story" },
{ placeID: 45, type: "Place" },// new added entry based on the next "place"
{ storyID: 20, place: 45, type: "Story" },
{ storyID: 22, place: 45, type: "Story" },
];
目前我已经将groupBy("place")
视为一种可能的方法,但这里的最终数组结构并不是我所需要的。
这是实现
const initalData = [
{ storyID: 1, place: 12, type: "Story" },
{ storyID: 5, place: 12, type: "Story" },
{ storyID: 99, place: 45, type: "Story" },
{ storyID: 8, place: 31, type: "Story" },
{ storyID: 16, place: 31, type: "Story" },
{ storyID: 20, place: 45, type: "Story" },
{ storyID: 22, place: 45, type: "Story" },
];
let oldPlace = null;
let resultData = [];
for (let row of initalData) {
if (row.place != oldPlace) {
resultData.push({ placeID: row.place, type: "Place" });
oldPlace = row.place;
}
resultData.push(row);
}
console.log (resultData);
我正在尝试在某个 属性 更改后向数组添加新条目,在我的例子中是“位置”更改。但是,我很难找到合适的方法,因为操作取决于数组中的下一个对象。
const initalData = [
{ storyID: 1, place: 12, type: "Story" },
{ storyID: 5, place: 12, type: "Story" },
{ storyID: 99, place: 45, type: "Story" },
{ storyID: 8, place: 31, type: "Story" },
{ storyID: 16, place: 31, type: "Story" },
{ storyID: 20, place: 45, type: "Story" },
{ storyID: 22, place: 45, type: "Story" },
];
const targetData = [
{ placeID: 12, type: "Place" }, // new added entry based on the next "place"
{ storyID: 1, place: 12, type: "Story" },
{ storyID: 5, place: 12, type: "Story" },
{ placeID: 45, type: "Place" }, // // new added entry based on the next "place"
{ storyID: 99, place: 45, type: "Story" },
{ placeID: 31, type: "Place" }, // new added entry based on the next "place"
{ storyID: 8, place: 31, type: "Story" },
{ storyID: 16, place: 31, type: "Story" },
{ placeID: 45, type: "Place" },// new added entry based on the next "place"
{ storyID: 20, place: 45, type: "Story" },
{ storyID: 22, place: 45, type: "Story" },
];
目前我已经将groupBy("place")
视为一种可能的方法,但这里的最终数组结构并不是我所需要的。
这是实现
const initalData = [
{ storyID: 1, place: 12, type: "Story" },
{ storyID: 5, place: 12, type: "Story" },
{ storyID: 99, place: 45, type: "Story" },
{ storyID: 8, place: 31, type: "Story" },
{ storyID: 16, place: 31, type: "Story" },
{ storyID: 20, place: 45, type: "Story" },
{ storyID: 22, place: 45, type: "Story" },
];
let oldPlace = null;
let resultData = [];
for (let row of initalData) {
if (row.place != oldPlace) {
resultData.push({ placeID: row.place, type: "Place" });
oldPlace = row.place;
}
resultData.push(row);
}
console.log (resultData);