javascript - 将两个对象转换为一个数组(JavaScript)

标签 javascript arrays

我有 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/

相关文章:

javascript - 使用 Node.js + Express 调用 HTTP GET 和 POST 请求,无需页面

Javascript 函数不返回结果

javascript - 如何在 Javascript 中访问 JSON 数组

带有 NumPy 和对象引用的 Python 3

c# - 三角形存储为数组。每层的高度和长度?

c# - 如何限制 Ajax 日历上的日期范围?

javascript - 使用 javascript 在 css 网格布局中突出显示元素

javascript - 将鼠标悬停在元素中包含的子输入文本字段上时,该元素触发mouseout或mouseleave事件

python - Numpy 数组中的循环和搜索

JAVA vector 打印