JavaScript

{"key":"hoge", "value":"aaa"}を、{"hoge":"aaa"}に変換

More than 3 years have passed since last update.

たまに必要になる。


js

var a = [

{key:"hello", value:"aaa"},
{key:"world", value:"bbb"},
{key:"test", value:"ccc"},
{key:"hoge", value:"ddd"}
];

var b = a.map(function(d){ var tmp={}; tmp[d.key]=d.value; return tmp });

console.log(b);
->"[{"hello":"aaa"},{"world":"bbb"},{"test":"ccc"},{"hoge":"ddd"}]"