我们如何将两个以上的对象数组合并到一个对象数组中

时间:2018-03-28 09:24:30

标签: javascript arrays node.js

如何将两个以上的对象数组合并到一个对象数组中。 就像我一样:

var arr1 = [{"userID": 554,"name":"abc"},{"userID": 555,"name":"xyz"}]    

var arr2 = [{"userID": 554,"lang":"ENG"},{"userID": 554,"lang":"GER"},{"userID": 555,"lang":"ENG"}]

var arr3 = [{"userID": 554,"SET":"QWE"},{"userID": 555,"SET":"ABC"},{"userID": 555,"SET":"XYZ"}]

现在我想通过相同的键值将所有这些合并为一个,即userID,所以我的最终输出应该是

var final = [
    {
        "name":"abc"
        "userID": 554,
        "arr2" = [
            {
                "userID": 554,
                "lang":"ENG"
            },
            {
                "userID": 554,
                "lang":"GER"
            }
        ],
        "arr3" = [
            {
                "userID": 554,
                "SET":"QWE"
            }
        ]
    },
    {
        "userID": 555,
        "name":"xyz"
        "arr2" = [
            {
                "userID": 555,
                "name":"ENG"
            }
        ],
        "arr3" = [
            {
                "userID": 555,
                "SET":"ABC"
            },
            {
                "userID": 555,
                "SET":"XYZ"
            }
        ]
    }
]

我无法使用Spread运算符(...)来使用ES6,因为我的条件是它应该在键userID匹配的数组中合并,以便我得到上面的输出或类似{{ 1}}

4 个答案:

答案 0 :(得分:1)

试试这个,应该给出准确的输出

let result = arr1.map(item =>{
    let obj = {};
    obj.userID = item.userID;
    obj.name = item.name;
    obj.arr2 = arr2.filter( it => it.userID === item.userID);
    obj.arr3 = arr3.filter( it => it.userID === item.userID);
    return obj;
})
console.log(result);

答案 1 :(得分:1)

根据@ Deshak9的回答



var arr1 = [{"userID": 554,"name":"abc"},{"userID": 555,"name":"xyz"}]    
var arr2 = [{"userID": 554,"lang":"ENG"},{"userID": 554,"lang":"GER"},{"userID": 555,"lang":"ENG"}]
var arr3 = [{"userID": 554,"SET":"QWE"},{"userID": 555,"SET":"ABC"},{"userID": 555,"SET":"XYZ"}]

let result = arr1.map(item => {
    let obj = {};
    obj.userID = item.userID;
    obj.name = item.name;
    obj.lang = arr2.filter( it => it.userID === item.userID).map(item => { return item.lang; });
    obj.SET = arr3.filter( it => it.userID === item.userID).map(item => { return item.SET; });
    return obj;
})
console.log(result);




我认为将所有值添加到给定属性的数组中会更具可读性。请参阅下面的示例。

{
  "userID": 554,
  "name": "abc",
  "lang": [
    "ENG",
    "GER"
  ],
  "SET": [
    "QWE"
  ]
}

答案 2 :(得分:0)

你可以使用

  1. ES6表示法

    var arr1 = [{“userID”:554,“name”:“abc”},{“userID”:555,“name”:“xyz”}]

    var arr2 = [{“userID”:554,“lang”:“ENG”},{“userID”:554,“lang”:“GER”},{“userID”:555,“lang”: “ENG”}]

    var arr3 = [{“userID”:554,“SET”:“QWE”},{“userID”:555,“SET”:“ABC”},{“userID”:555,“SET”: “XYZ”}]

    const arr = [... arr1,... arr2,... arr3];

  2. Array.prototype.contact()

答案 3 :(得分:0)

使用ES6,使用Spread运算符(...),

非常容易



var arr1 = [{"userID": 554,"name":"abc"},{"userID": 555,"name":"xyz"}]    

var arr2 = [{"userID": 554,"lang":"ENG"},{"userID": 554,"lang":"GER"},{"userID": 555,"lang":"ENG"}]

var arr3 = [{"userID": 554,"SET":"QWE"},{"userID": 555,"SET":"ABC"},{"userID": 555,"SET":"XYZ"}]

const merged = [].concat(...arr1,...arr2,...arr3)
console.log(merged)

//Another way of doing is,

const merged1 = [...arr1 , ...arr2,...arr3];
console.log(merged1)