繁体   English   中英

Angular 2如何将对象数组放入另一个对象数组

[英]Angular 2 how to put object array in to another object array

所以我有2 Arrays (总是等于index ):

A: [
{name: Jhon1}
{name: Jhon2}
{name: Jhon3}
]

B: [
{lastName: Pom1}
{lastName: Pom2}
{lastName: Pom3}
]

合并后的预期结果:

A: [
   {name: Jhon1, lastName: Pom1}
   {name: Jhon2, lastName: Pom2}
   {name: Jhon3, lastName: Pom3}
]

Concat方法只是将整个数组合并为一个这样的数组:

  A: [
    {name: Jhon1}
    {name: Jhon2}
    {name: Jhon3}
    {lastName: Pom1}
    {lastName: Pom2}
    {lastName: Pom3}
    ]

我正在尝试的我自己的功能。 下面有两个数组: this.pricesthis.search.favoriteItems ,然后我想像以前描述的那样合并:

showProducts(){
   for(let i of this.localStorageArray){
    let id = localStorage.getItem(i);
    this.search.getProductsById(id).subscribe
    ((data: any) => {
     this.prices.push(data.lowestPrices.Amazon);
     this.search.favoriteItems.push(data);  
   //something here to merge this.prices and this.search
    });

  }
  }

您可以将.map()与对象销毁一起使用:

 let arr1 = [{name: 'Jhon1'}, {name: 'Jhon2'}, {name: 'Jhon3'}], arr2 = [{lastName: 'Pom1'}, {lastName: 'Pom2'}, {lastName: 'Pom3'}]; let zip = (a1, a2) => a1.map((o, i) => ({...o, ...a2[i]})); console.log(zip(arr1, arr2)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM