Home > Enterprise >  How to convert array of object property?
How to convert array of object property?

Time:01-19

I'm trying to convert array of objects having value as a array -> into string. I'm facing blocker in this,

    let peopleDetails = [
     {
       name: "raj",
       favCar: [{ name: "audi", color: "white" }],
       favFood: [{ color: "brown", name: "Idli" }],
     },
     { name: "deepak", place: "India", favPlace: [{ name: "Tajmahal" }] },
    ];

I need structure like,

    let peopleDetails = [
     { name: "raj", favCar: "audi", favFood: "Idli" },
     { name: "deepak", place: "India", favPlace: "Tajmahal" },
    ];

CodePudding user response:

For what I understand you want the value of every property to become the name value of its first element, when the value is an array of object.

Here's an immutable solution that follows this concept:

let peopleDetails = [
    {
        name: 'raj',
        favCar: [{ name: 'audi', color: 'white' }],
        favFood: [{ name: 'Idli' }],
    },
    { name: 'deepak', place: 'India', favPlace: [{ name: 'Tajmahal' }] },
];


const result = peopleDetails.map(obj => 
  Object.fromEntries(Object.entries(obj).map(([key, value]) => 
    [key, value?.[0]?.name ?? value]
  ))
);

console.log(result);

CodePudding user response:

I'm not sure why you've got arrays of singular elements (favourites typically don't have second or third places) so if you just want to extract the name from each item that's an array, take it from the first element in each array:

let peopleDetails = [{
    name: "raj",
    favCar: [{
      name: "audi",
      color: "white"
    }],
    favFood: [{
      name: "Idli"
    }],
  },
  {
    name: "deepak",
    place: "India",
    favPlace: [{
      name: "Tajmahal"
    }]
  },
];

let peopleDetailsModified = peopleDetails.map(o => {
  let retObj = {};
  for (key in o) {
    if (Array.isArray(o[key])) {
      retObj[key] = o[key][0].name;
    } else {
      retObj[key] = o[key];
    }
  }
  return retObj;
});

console.log(peopleDetailsModified);

I've made this code more verbose than it needs to be, it's quite easy to one-line it using reduce.

CodePudding user response:

I'm also quite not sure what you're trying to achieve as the other answers but I tried to make a map of the current object to an object that is more to your liking. I formatted it a little so you can see what I did:

peopleDetails.map(item =>  {
return {
name: item.name, 
place: item.place, 
favCar: item.favCar ? item.favCar[0].name : "", 
favPlace: item.favPlace ? item.favPlace[0].name : "", 
favFood: item.favFood ? item.favFood[0].name : ""}});

CodePudding user response:

I have ran the code and its a generic code for similar data

const _ = require('lodash')
const peopleDetails =[
  {
    "name": "raj",
    "favCar": [
      {
        "name": "audi",
        "color": "white"
      }
    ],
    "favFood": [
      {
        "name": "Idli"
      }
    ]
  },
  {
    "name": "deepak",
    "place": "India",
    "favPlace": [
      {
        "name": "Tajmahal"
      }
    ]
  }
]

const newPeopleDetails = peopleDetails.map(obj => {
    const formObj = {
        favCar: obj && obj.favCar && obj.favCar[0].name,
        favFood: obj && obj.favFood && obj.favFood[0].name,
        favPlace: obj && obj.favPlace && obj.favPlace[0].name
      }
      const finalObj = _.pickBy(formObj, _.identity);
  return Object.assign({}, obj, finalObj);
});
console.log(newPeopleDetails)
  • Related