Arshad Rehmani
Arshad Rehmani

Reputation: 2185

Convert array of JSON object to a js map

This is an additional question to find a value inside array of JSON object

I get below Array of JSON objects from JSP

"Titles":[                          
    {
    "Book3" : "BULLETIN 3"
    }   
    ,
    {
    "Book1" : "BULLETIN 1"
    }
    ,
    {
    "Book2" : "BULLETIN 2"
    }    
]

On JS side, it is parsed and I see an array with 3 objects.

Now, I want to convert this array of objects into below JS map.

newTitles["Book3"] = "BULLETIN 3";
newTitles["Book1"] = "BULLETIN 1";
newTitles["Book2"] = "BULLETIN 2";

where newTitles is a js Object created via new Object command.

Upvotes: 0

Views: 163

Answers (1)

CD..
CD..

Reputation: 74096

Like this:

var newTitles = {};

Titles.forEach(function(obj){
  var key = Object.keys(obj)[0];
  newTitles[key] = obj[key];
});

Upvotes: 2

Related Questions