Map array of key/value pairs to a Javascript object
Want to convert this
var temp = [
{
"name" : "apple",
"value" : "A for apple"
},
{
"name" : "ball",
"value" : "B for ball"
},
{
"name" : "cat",
"value" : "C for cat"
}
]
to
{
apple: "A for apple",
ball: "B for ball",
cat: "C for cat"
}
?
Here's how you can do it with underscores.js
temp = _.object(_.map(temp, function(x){return [x.name, x.value]}))
Written by Ashwin
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#