Alex Ironside
Alex Ironside

Reputation: 5039

Merging objects with multiple properties in JS

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

Upvotes: 0

Views: 134

Answers (3)

Alex Ironside
Alex Ironside

Reputation: 5039

The below answers for some reason didn't transfer the boothVisits properly. I ended up doing this:

const boothVisitsByEmp = employers.map(emp => {
    const employerName = empNames.find(name => name.id.equals(emp.id));
    emp.companyName = employerName.companyName;
    return emp;
});

Upvotes: 0

Randy Casburn
Randy Casburn

Reputation: 14165

An alternative, slightly more readable solution, is below. Essentially, find the index of the names array of object and assign that name to the booths object.

let a = [{"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"]}],
    b = [{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 out = a.map(objA => {
  return b.map(objB => {
      let idxB = Object.entries(objB)[0].findIndex(element => element == objA.id);
      if(idxB > 0) objA.companyName = objB.companyName;
      return objA;
  });
});
console.log(out);

Upvotes: 1

Mohammad Usman
Mohammad Usman

Reputation: 39322

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"]}],
    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), r
), {}));

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

Upvotes: 1

Related Questions