合并JS中具有多个属性的对象

时间:2018-11-04 13:48:09

标签: javascript arrays object merge javascript-objects

我有这些数组

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

我需要加入它们,以便companyName和boothVisits都是数组中新对象的一部分。有点像SQL连接。但是我不知道该怎么做。我发现this answer

并试图改变其用途

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)

在这里满足我的需求:

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

但是没有运气。知道如何解决这个问题吗?

一些预期的输出:

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

3 个答案:

答案 0 :(得分:1)

您可以使用.reduce()方法获取结果对象,并使用Object.values()获取所需的数组:

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

答案 1 :(得分:1)

下面是一个替代的,更具可读性的解决方案。本质上,找到对象names数组的索引,然后将该名称分配给booths对象。

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

答案 2 :(得分:0)

由于某些原因,以下答案未正确转移展位。我最终这样做:

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