[英]Converting JSON name/value to array with object literals
I'm looking to convert key-value JSON code that looks like this : 我正在寻找转换像这样的键值JSON代码:
{
"AK": "Arkansas",
"NY": "New York",
"CA": "California"
}
Into an array, where the key value from the JSON becomes one of the values in the object literals. 放入数组,其中JSON的键值成为对象文字中的值之一。
0 : {code :"AK", name:"Arkansas"},
1 : {code:"NY", name:"New York"},
2 : {code:"CA", name:"California}
Is there an easy way to do this? 是否有捷径可寻?
Use this code: 使用此代码:
var obj = {
"AK": "Arkansas",
"NY": "New York",
"CA": "California"
}
var array1 = [];
for (key in obj) {
array1.push({
"code": key,
"name": obj[key]
});
}
console.log(array1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.