将一个对象数组转换为另一个对象数组



我目前正在努力转换对象数组以满足我的需求。

我的初始数组是这样的:

[{
"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
}]

所以基本上是"one_answers";carBrand"属性将出现在每个新对象上,以及剩下的每个属性。我用lodash试过各种各样的场景,但我就是做不到。欢迎提出任何建议和提示,谢谢。

您可以使用flatMapObject.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);

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)

解构在这里很有用,它可以隔离你知道你想要的属性,同时使对象的...rest保持完整,以供进一步迭代。

这里在外循环中隔离adIdcarBrand,然后迭代对象其余部分的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; }

最新更新