我有 2 个具有相同键的 JS(json) 对象:
{"id1" : "some1", "id2" : "some2", "id3" : "some3"}
{"id1" : "another1", "id2" : "another2", "id3" : "another3"}
我需要将其转换为
[
{
"id" : "id1",
"some" : "some1",
"another" : "another1"
},
{
"id" : "id2",
"some" : "some2",
"another" : "another2"
},
{
"id" : "id3",
"some" : "some3",
"another" : "another3"
}
]
所以,这就是问题。
谢谢!
最佳答案
Here你走:
var some = {"id1" : "some1", "id2" : "some2", "id3" : "some3"};
var another = {"id1" : "another1", "id2" : "another2", "id3" : "another3"};
var result = [];
for (var id in some) {
result.push({
id: id,
some: some[id],
another: another[id]
});
}
alert(JSON.stringify(result));
关于javascript - 将两个对象转换为一个数组(JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032133/