我通过data
Observable
以下的设置
[
0: {id: "12321", itemName: "Item 1", category: "All"},
1: [
0: {id: "423423", itemName: "Sub Item 1", category: "subcat"},
1: {id: "413212", itemName: "Sub Item 2", category: "subcat"}
],
2: {id: "65655", itemName: "Item 2", category: "All"},
3: {id: "87877", itemName: "Item 3", category: "All"},
4: [
0: {id: "354345", itemName: "Sub Item 1", category: "subcat"},
1: {id: "123434", itemName: "Sub Item 2", category: "subcat"},
2: {id: "765767", itemName: "Sub Item 3", category: "subcat"},
3: {id: "854643", itemName: "Sub Item 4", category: "subcat"},
]
]
我正在尝试将objects
内的所有array
移至root
数组。预期的结果是
[
0: {id: "12321", itemName: "Item 1", category: "All"},
1: {id: "423423", itemName: "Sub Item 1", category: "subcat"},
2: {id: "413212", itemName: "Sub Item 2", category: "subcat"}
3: {id: "65655", itemName: "Item 2", category: "All"},
4: {id: "87877", itemName: "Item 3", category: "All"},
5: {id: "354345", itemName: "Sub Item 1", category: "subcat"},
6: {id: "123434", itemName: "Sub Item 2", category: "subcat"},
7: {id: "765767", itemName: "Sub Item 3", category: "subcat"},
8: {id: "854643", itemName: "Sub Item 4", category: "subcat"},
]
这是数据在控制台上的显示方式
以下是我迄今为止所尝试过的内容。
lstCategories: Observable<any>;
this.lstCategories = .....
.flatMap(records => Observable.combineLatest(records))
.map(da => {
return da.map(mda => {
//if it is array then map each object in array.
if(mda.length){
return mda.map(smda => {
return Observable.of(smda);
})
}else {
return mda;
}
})
});
console.log(da)
,即第一个map
显示我正在获取的数据类型。我正在检查length
中每个element
的{{1}}并将其映射回来。
我在这里失踪的是什么?
答案 0 :(得分:4)
根据图像,您有一个对象和数组的数组。您可以使用[].concat(...data)
展平它。使用带有扩展语法的array#concat
。
var data = [{id: "12321", itemName: "Item 1", category: "All"}, [{id: "423423", itemName: "Sub Item 1", category: "subcat"}, {id: "413212", itemName: "Sub Item 2", category: "subcat"}],{id: "65655", itemName: "Item 2", category: "All"}, {id:"87877", itemName: "Item 3", category: "All"}, [{id: "354345", itemName: "Sub Item 1", category: "subcat"}, {id: "123434", itemName: "Sub Item 2", category: "subcat"}, {id: "765767", itemName: "Sub Item 3", category: "subcat"}, {id: "854643",itemName: "Sub Item 4", category: "subcat"}]],
result = [].concat(...data);
console.log(result);
&#13;