0

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

7
  • 2
    If the question is Can we?, Yes we can. Commented Mar 28, 2018 at 9:25
  • You have a typo in the second array, name should be lang I suppose? else I think it is confusing... Commented Mar 28, 2018 at 9:28
  • @KungWaz Thanks, I have updated Commented Mar 28, 2018 at 9:30
  • Possible duplicate of es6 using spread to concat multiple arrays Commented Mar 28, 2018 at 9:31
  • Why do you want to add arr2 and arr3 as props with arrays? Wouldn't it be more readable to have {"userID": 554, "name": "abc", "lang": ["ENG", "GER"],...} Commented Mar 28, 2018 at 9:37

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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)

2 Comments

When I run the snippet I get, { "message": "Uncaught SyntaxError: Identifier 'merged' has already been declared",
I need to merge it on the basis of same userID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.