Home > Blockchain >  Transform array of objects to another array of objects
Transform array of objects to another array of objects

Time:10-28

i am currently struggling to transform an array of objects to fit my needs.

My initial array is looking like this:

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

I would like that my new array look like this:

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

So basically "adId" and "carBrand" properties would be present on every new object together with each one of the properties that is left. I've tried various scenarios with lodash but i simply can't make it. Any suggestions and hints are welcome, cheers.

CodePudding user response:

forEach can help you out:

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)

CodePudding user response:

You can easily achieve the result using flatMap and Object.entries

One-liner

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Destructuring is useful here to isolate the properties you know you want while leaving the ...rest of the object intact for further iteration. Here isolating adId and carBrand in the outer loop, and the iterating the Object.entries() of the rest of the object to construct the new objects to be push()ed into the result array.

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; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related