Sonu
Sonu

Reputation: 29

How can we merge more then two array of object into one array of objects

How can I merge more than two array of objects into one array of objects. Like I have:

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"}]

Now I want to merge all these into one by the same key value i.e., userID, so my final output should be like

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"
            }
        ]
    }
]

I can't user ES6 with Spread operator (...) as I have a condition that it should be merged in the array where the key userID matches so that I gets the above output or like {"userID": 554, "name": "abc", "lang": ["ENG", "GER"],...}

Upvotes: 0

Views: 106

Answers (4)

KungWaz
KungWaz

Reputation: 1956

Based on the answer from @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);

I think it makes it more readable to add all the values to an array for the given property. See the example below.

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

Upvotes: 1

prabhatojha
prabhatojha

Reputation: 2085

try this one, should give exact output

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);

Upvotes: 1

Vijayanath Viswanathan
Vijayanath Viswanathan

Reputation: 8541

With ES6 it is pretty easy with Spread operator (...),

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)

Upvotes: 0

Webvoid
Webvoid

Reputation: 511

You could just use

  1. ES6 notation

    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()

Upvotes: 0

Related Questions