最简单的方法来展平具有此类型接口的对象数组:
Interface {
Item: Item,
SubItems: Array<Item>
}
数组示例:
myArray = [
{Item: {id: "bla1"}, SubItems: [{id: "bla2"}, {id: "bla3"}, {id: "bla4"}]},
{Item: {id: "bla5"}, SubItems: [{id: "bla6"}, {id: "bla7"}, {id: "bla8"}]}
]
最终结果应该是所有这些相同类型的对象的单个数组,并按以下顺序排序:
first object -> Item -> SubItems
second object -> Item -> SubItems
所以在我的示例中,我应该有这个:
flattenedArray = [{id: "bla1"}, {id: "bla2"}, {id: "bla3"}, {id: "bla4"}, ...]
答案 0 :(得分:1)
myArray.map(val => [val.Item, ...val.SubItems]).reduce((acc, cur) => [...acc, ...cur])