-4

I have an object which looks like this.

{
   "A": [ "1", "2", "3" ]
}  

I want to manipulate the object to get the following result:

{
    "A": [{
        "A": "1"
    }, {
        "A": "2"
    }, {
        "A": "3"
   }]
}

What is the way to achieve this?

arun
  • 1
  • 1

2 Answers2

0

As @MelanciaUK has mentioned, you will have to write code. There isn't just such a convert method you will have to call.

The following example will work in your usecase.

var myObj = {
   "A": [ "1", "2", "3" ]
}

//convert it
myObj = {
    "A": [{
        "A": "1"
    }, {
        "A": "2"
    }, {
        "A": "3"
   }]
}

//print result
console.log(myObj);
Simon Schüpbach
  • 2,625
  • 2
  • 13
  • 26
0

If you want to know how to convert that,

var myObj = {
   "A": [ "1", "2", "3" ]
} // this is the object you want to convert

var newObj = {}; //create a new empty object

newObj.A = [];// set a Key "A" of newObj to an empty Array.

for (i = 0; i < myObj.A.length; i++) //loop through the initial object and convert it
    {

     newobj.A[i] = {"A":myObj.A[i]} //for every iteration, add an object to the empty array.(newObj.A)
};
code
  • 2,115
  • 1
  • 22
  • 46