简体   繁体   中英

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.

Like this:

var newTitles = {};

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM