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

Transform array of objects to another array of objects

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

我的初始数组如下所示:

 [{
    "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 尝试了各种场景,但我就是做不到。欢迎任何建议和提示,干杯。

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)

您可以使用 flatMap and 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);

Destructuring 在这里很有用,可以隔离您想要的属性,同时让对象的 ...rest 保持完整以供进一步迭代。

这里在外层循环中隔离了adIdcarBrand,然后迭代Object.entries() of the rest of the object to construct the new objects. Computed property assignment allows us to assign to a property from a variable (the k from the iterator here). Each new object is then pushed into the result array with 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; }