-1

This is my array of Object. I want to convert my first value into key & Second value into value only. Please go through question and i have also attached my desired output.

 [
  {
    "name": "Unknown",
    "value": "RAHUL"
  },
  {
    "name": "FirstName",
    "value": "WILLEKE LISELOTTE"
  },
  {
    "name": "LastName",
    "value": "DE BRUIJN"
  }
]

I want my Object as

{
"Unknown": "RAHUL"

},
{
"FirstName": "WILLEKE LISELOTTE"
},
{
"LastName": "DE BRUIJN"
}
Rahul More
  • 514
  • 6
  • 10

2 Answers2

0

IF you mean to create a new array, then simply map the original array. You don't need to create a new array and push into it, that's just redundant code:

const testData = [{
    "name": "Unknown",
    "value": "RAHUL"
  },
  {
    "name": "FirstName",
    "value": "WILLEKE LISELOTTE"
  },
  {
    "name": "LastName",
    "value": "DE BRUIJN"
  }
];

const newData = testData.map(nameObject => {
  return {
    [nameObject.name]: nameObject.value
  }
});
console.log(newData);
StudioTime
  • 22,603
  • 38
  • 120
  • 207
-1

You just have to use brackets for [value.name] to perform achieve this.

var arr = []

testdata.map((value) => {
    arr.push({ [value.name]: value.value })
})
Rahul More
  • 514
  • 6
  • 10