我目前正在努力轉換一系列物件以滿足我的需求。
我的初始陣列如下所示:
[{
"adId": "uuid"
"carBrand": "audi",
"carColor: "blue",
"carType": "sedan"
"yearOfProduction": 1999,
"price": 10.000
},{
"adId": "uuid"
"carBrand": "mercedes",
"carColor: "yellow",
"carType": "coupe"
"yearOfProduction": 2004,
"price": 14.000
},{
"adId": "uuid"
"carBrand": "bmw",
"carColor: "green",
"carType": "minivan"
"yearOfProduction": 2007,
"price": 6.000
}]
我希望我的新陣列看起來像這樣:
[{
"adId": "uuid"
"carBrand": "audi",
"carColor: "blue"
},{
"adId": "uuid"
"carBrand": "audi",
"carType: "sedan"
},{
"adId": "uuid"
"carBrand": "audi",
"yearOfProduction: "1999"
},{
"adId": "uuid"
"carBrand": "audi",
"price: 10.000
},
{
"adId": "uuid"
"carBrand": "mercedes",
"carColor: "yellow"
},{
"adId": "uuid"
"carBrand": "mercedes",
"carType: "coupe"
},{
"adId": "uuid"
"carBrand": "mercedes",
"yearOfProduction: "2004"
},{
"adId": "uuid"
"carBrand": "mercedes",
"price: 14.000
},
{
"adId": "uuid"
"carBrand": "bmw",
"carColor: "green"
},{
"adId": "uuid"
"carBrand": "bmw",
"carType": "minivan"
},{
"adId": "uuid"
"carBrand": "bmw",
"yearOfProduction": 2007,
},{
"adId": "uuid"
"carBrand": "bmw",
"price": 6.000
}]
所以基本上“adId”和“carBrand”屬性將與每個剩下的屬性一起出現在每個新物件上。我已經用 lodash 嘗試了各種場景,但我就是做不到。歡迎任何建議和提示,干杯。
uj5u.com熱心網友回復:
forEach 可以幫助您:
const array = [{
"adId": "uuid",
"carBrand": "audi",
"carColor": "blue",
"carType": "sedan",
"yearOfProduction": 1999,
"price": 10.000
},{
"adId": "uuid",
"carBrand": "mercedes",
"carColor": "yellow",
"carType": "coupe",
"yearOfProduction": 2004,
"price": 14.000
},{
"adId": "uuid",
"carBrand": "bmw",
"carColor": "green",
"carType": "minivan",
"yearOfProduction": 2007,
"price": 6.000
}]
const newArray = []
array.forEach(el=> {
newArray.push({adId: el.adId, carBrand: el.carBrand, carColor: el.carColor})
newArray.push({adId: el.adId, carBrand: el.carBrand, carType: el.carType})
newArray.push({adId: el.adId, carBrand: el.carBrand, yearOfProduction: el.yearOfProduction})
newArray.push({adId: el.adId, carBrand: el.carBrand, price: el.price})
})
console.log(newArray)
uj5u.com熱心網友回復:
您可以使用flatMap和輕松實作結果Object.entries
單線
arr.flatMap(({ adId, carBrand, ...rest }) => Object.entries(rest).map(([k, v]) => ({ adId, carBrand, [k]: v })))
const arr = [
{
adId: "uuid",
carBrand: "audi",
carColor: "blue",
carType: "sedan",
yearOfProduction: 1999,
price: 10.0,
},
{
adId: "uuid",
carBrand: "mercedes",
carColor: "yellow",
carType: "coupe",
yearOfProduction: 2004,
price: 14.0,
},
{
adId: "uuid",
carBrand: "bmw",
carColor: "green",
carType: "minivan",
yearOfProduction: 2007,
price: 6.0,
},
];
const result = arr.flatMap(({ adId, carBrand, ...rest }) => {
return Object.entries(rest).map(([k, v]) => ({ adId, carBrand, [k]: v }));
});
console.log(result);
uj5u.com熱心網友回復:
解構在這里很有用,它可以隔離您知道想要的屬性,同時保持...rest物件的完整以供進一步迭代。
在這里隔離adId和carBrand在外回圈中,然后迭代Object.entries()物件的其余部分以構造新物件。計算屬性賦值允許我們從變數(k這里的迭代器)中賦值給屬性。然后將每個新物件推送到結果陣列中push()。
const arr = [{ adId: 'uuid', carBrand: 'audi', carColor: 'blue', carType: 'sedan', yearOfProduction: 1999, price: 10.0, }, { adId: 'uuid', carBrand: 'mercedes', carColor: 'yellow', carType: 'coupe', yearOfProduction: 2004, price: 14.0, }, { adId: 'uuid', carBrand: 'bmw', carColor: 'green', carType: 'minivan', yearOfProduction: 2007, price: 6.0, },];
const result = [];
for (const { adId, carBrand, ...rest } of arr) {
for (const [k, v] of Object.entries(rest)) {
result.push({ adId, carBrand, [k]: v });
}
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/339341.html
標籤:javascript 数组 目的 洛达什
