0

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"]
}]
4
  • Can you please specify which variable is what? I don't know what is event, boothVisitsByEmp, empNames... Commented Nov 4, 2018 at 13:52
  • I'd suggest providing examples of expected merge result/output as well Commented Nov 4, 2018 at 13:55
  • I edited my post. BoothVisitsByEmp is supposed to be the merged array Commented Nov 4, 2018 at 13:55
  • I edited the question Commented Nov 4, 2018 at 13:59

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

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.