所以我有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.prices
和this.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
});
}
}
答案 0 :(得分:5)
您可以将.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; }