[英]How to to put the value of a key in an object in an array based on the order of another array
[英]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.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
});
}
}
您可以將.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.