RxJ将对象数组移动到根数组 [英] RxJs move array of objects to root array

查看:60
本文介绍了RxJ将对象数组移动到根数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过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"},
  ]
]

我正在尝试将array中的所有objects移至root数组.预期结果是

I am trying to move all the objects within array to root array. The expected result is

[
  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"},
]

这是数据在控制台上的显示方式

以下是我到目前为止尝试过的内容.

Below is what I've and tried so far.

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显示我获取的数据类型.我正在检查array中每个elementlength并将其映射回去.

console.log(da) i.e. first map displays the type of data I am getting. I am checking length of each element in array and mapping it back.

我在这里想念什么?

推荐答案

基于图像,您有一个对象数组和一个数组.您可以使用[].concat(...data)将其展平.结合使用array#concat和扩展语法.

Based on the image, you have an array of objects and array. You can flatten it using [].concat(...data). Use array#concat with spread syntax.

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

这篇关于RxJ将对象数组移动到根数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆