0

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.

1 Answer 1

2

Like this:

var newTitles = {};

Titles.forEach(function(obj){
  var key = Object.keys(obj)[0];
  newTitles[key] = obj[key];
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.