-3

I have a json Object like following.

{987: "sqa", 988: "Squad"}

I need to change the structure like following

[{id:987, value:'sqa'}, {id:988, value:'Squad'}]

How to do this with Javascript?

Arun Reghu
  • 59
  • 1
  • 5

3 Answers3

2

You could get the entries of the object and map new objects in an array.

Techniques:

var object = { 987: "sqa", 988: "Squad" },
    result = Object.entries(object).map(([id, value]) => ({ id, value }));
   
console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You have written invalid JSON in your question, but I assume you want it as a JavaScript object.

Something like this perhaps.

var oldObj = {
    987: "sqa",
    988: "Squad"
}

var newArray = [];

for (let k in oldObj) {
    newArray.push({'id': k, 'value': oldObj[k]});
}

console.log(newArray);
Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
0
const intial = {987: "sqa", 988: "Squad"}  
const final = [];
for(let idNum in intial){
  const data = {
    id: idNum,
    value: intial[idNum]
   }
  final.push(data);
}
console.log(final);  //[ { id: '987', value: 'sqa' }, { id: '988', value: 'Squad' } ]

This will achieve what you're trying to do

Aditya Dan.
  • 108
  • 8