这是我的输入:
输入可能会收到更多数据,如 'e:5'
var input = {a:2,b:3,c:2,d:1};
我想将此输入转换为以下输出:
var output = [{name="a",id="2"},
{name="b",id="3"},
{name="c",id="2"},
{name="d",id="1"}];
提前致谢。
最佳答案
The
Object.keys()
method returns an array of a given object's own enumerable properties.
The
map()
method creates a new array with the results of calling a provided function on every element in this array.
var input = {
a: 2,
b: 3,
c: 2,
d: 1
};
var mapped = Object.keys(input).map(function(key) {
return {
name: key,
id: input[key]
};
});
console.log(mapped);
关于javascript - 将javascript关联数组转换为多对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849303/