Merging objects with multiple properties in JS

2931 views javascript
6

I have these arrays

Event.attendants.employers = [
    {
        "id": "5bcda6477e51de001576d287",
        "boothVisits": ["5bcda6477e51de001576d287", "5bc9d28270e5375dfbfe17fd", "5bcdabd27e51de001576d289", "5bcdabd27e51de001576d289", "5bcdabd27e51de001576d289", "5bd067a9947f790015856ccb", "5bd067a9947f790015856ccb", "5bcdabd27e51de001576d289", "5bcda6477e51de001576d287"]
    },
    {
        "id": "5bc9d28270e5375dfbfe17fd",
        "boothVisits": ["5bc9d28270e5375dfbfe17fd", "5b4751df76ebd145d6c1dc2a", "5bcda6477e51de001576d287", "5bcdabd27e51de001576d289", "5bd067a9947f790015856ccb"]
    },
    {
        "id": "5bcda6477e51de001576d287", "boothVisits": []
    },
    {
        "id": "5bd05e35947f790015856cc8",
        "boothVisits": ["5bd05e35947f790015856cc8", "5bcdabd27e51de001576d289", "5bd05e35947f790015856cc8", "5bd067a9947f790015856ccb", "5bd0787e0f64cd001563ddbf", "5bd05e35947f790015856cc8", "5bcdabd27e51de001576d289", "5bd087570f64cd001563ddc5"]
    },
    {
        "id": "5bd05e35947f790015856cc8", "boothVisits": []
    },
    {
        "id": "5bd06524947f790015856cca",
        "boothVisits": ["5bc9d28270e5375dfbfe17fd", "5bd067a9947f790015856ccb"]
    },
    {
        "id": "5bd067a9947f790015856ccb",
        "boothVisits": ["5bd067a9947f790015856ccb", "5bd067a9947f790015856ccb", "5bd067a9947f790015856ccb", "5bd06321947f790015856cc9"]
    },
    {"id": "5bc9d28270e5375dfbfe17fd", "boothVisits": []}
]

empNames = [
    {id: 5bcda6477e51de001576d287, companyName: 'Renia'},
    {id: 5bc9d28270e5375dfbfe17fd, companyName: 'BSA Concept'},
    {id: 5bcda6477e51de001576d287, companyName: 'Renia'},
    {id: 5bd05e35947f790015856cc8, companyName: 'company name'},
    {id: 5bd05e35947f790015856cc8, companyName: 'company name'},
    {id: 5bd06524947f790015856cca, companyName: 'Roger'},
    {id: 5bd067a9947f790015856ccb, companyName: 'Reniax'},
    {id: 5bc9d28270e5375dfbfe17fd, companyName: 'BSA Concept'}
]

And I need to join them so that both companyName and boothVisits are a part of the new object in the array. Kind of like an SQL join. But I have no idea how to do it. I found this answer

And tried to re purpose it's code

var messages = [{userId: 2, content: "Salam"}, {userId: 5, content: "Hello"},{userId: 4, content: "Moi"}];
var users = [{id: 2, name: "Grace"}, {id: 4, name: "Janetta"},{id: 5, name: "Sara"}];

var messagesWithUserNames = messages.map((msg)=> {
  var haveEqualId = (user) => user.id === msg.userId
  var userWithEqualId= users.find(haveEqualId)
  return Object.assign({}, msg, userWithEqualId)
})
console.log(messagesWithUserNames)

To work for my needs here:

var boothVisitsByEmp = empNames.map((name) => {
    var haveEqualId = (user) => user.id === name.userId;
    var userWithEqualId = event.attendants.employers.find(haveEqualId);
    return Object.assign({}, name, userWithEqualId);
});

But no luck. Any idea how to solve this?

Some of the expected output:

var expectedOutput = [
{
    "id": "5bcda6477e51de001576d287",
    "companyName": 'Renia',
    "boothVisits": ["5bcda6477e51de001576d287", "5bc9d28270e5375dfbfe17fd", "5bcdabd27e51de001576d289", "5bcdabd27e51de001576d289", "5bcdabd27e51de001576d289", "5bd067a9947f790015856ccb", "5bd067a9947f790015856ccb", "5bcdabd27e51de001576d289", "5bcda6477e51de001576d287"]
},
{
    "id": "5bc9d28270e5375dfbfe17fd",
    "companyName": 'BSA Concept',
    "boothVisits": ["5bc9d28270e5375dfbfe17fd", "5b4751df76ebd145d6c1dc2a", "5bcda6477e51de001576d287", "5bcdabd27e51de001576d289", "5bd067a9947f790015856ccb"]
}]

answered question

Can you please specify which variable is what? I don't know what is event, boothVisitsByEmp, empNames...

I'd suggest providing examples of expected merge result/output as well

I edited my post. BoothVisitsByEmp is supposed to be the merged array

I edited the question

1 Answer

11

You can use .reduce() method to get a resultant object and use Object.values() to get the required array:

let arr1 = [
  {"id": "5bcda6477e51de001576d287", "boothVisits": ["5bcda6477e51de001576d287", "5bc9d28270e5375dfbfe17fd", "5bcdabd27e51de001576d289", "5bcdabd27e51de001576d289", "5bcdabd27e51de001576d289", "5bd067a9947f790015856ccb", "5bd067a9947f790015856ccb", "5bcdabd27e51de001576d289", "5bcda6477e51de001576d287"]},
  {"id": "5bc9d28270e5375dfbfe17fd", "boothVisits": ["5bc9d28270e5375dfbfe17fd", "5b4751df76ebd145d6c1dc2a", "5bcda6477e51de001576d287", "5bcdabd27e51de001576d289", "5bd067a9947f790015856ccb"]},
  {"id": "5bd05e35947f790015856cc8", "boothVisits": ["5bd05e35947f790015856cc8", "5bcdabd27e51de001576d289", "5bd05e35947f790015856cc8", "5bd067a9947f790015856ccb", "5bd0787e0f64cd001563ddbf", "5bd05e35947f790015856cc8", "5bcdabd27e51de001576d289", "5bd087570f64cd001563ddc5"]},
  {"id": "5bd06524947f790015856cca", "boothVisits": ["5bc9d28270e5375dfbfe17fd", "5bd067a9947f790015856ccb"]},
  {"id": "5bd067a9947f790015856ccb", "boothVisits": ["5bd067a9947f790015856ccb", "5bd067a9947f790015856ccb", "5bd067a9947f790015856ccb", "5bd06321947f790015856cc9"]}
];

let arr2 = [
    {id: '5bcda6477e51de001576d287', companyName: 'Renia'},
    {id: '5bc9d28270e5375dfbfe17fd', companyName: 'BSA Concept'},
    {id: '5bcda6477e51de001576d287', companyName: 'Renia'},
    {id: '5bd05e35947f790015856cc8', companyName: 'company name'},
    {id: '5bd05e35947f790015856cc8', companyName: 'company name'},
    {id: '5bd06524947f790015856cca', companyName: 'Roger'},
    {id: '5bd067a9947f790015856ccb', companyName: 'Reniax'},
    {id: '5bc9d28270e5375dfbfe17fd', companyName: 'BSA Concept'}
];

let result = Object.values(
  [...arr1, ...arr2].reduce((r, c) => {
    r[c.id] = Object.assign((r[c.id] || {}), c);
    return r;
  }, {})
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

posted this

Have an answer?

JD

Please login first before posting an answer.