Merge subarrays using Observables

后端 未结 2 1264
执笔经年
执笔经年 2020-12-02 03:06

I have this data structure:

[{
    id : 1,
    name : \"Item 1\",
    subItems : [{
            id : 1,
            name : \"SubItem 1\"
        },{
                 


        
2条回答
  •  旧巷少年郎
    2020-12-02 03:49

    Provided it is a typo and the second element has subItems as well (and not searchProfiles), you don't need flatMap or any thing of the sort, you can do it in a plain map using js array operators:

    var transformed = [].concat(...result.map(item => item.subItems));
    

    or in your case

    httpResult$.map(result => [].concat(...result.map(item => item.subItems))
    

    if the use of different keys is deliberate, your internal map will require a bit more logic but the mapping will be quite the same

提交回复
热议问题