合并两个数组并展平其中一个内部对象

Merge two arrays and flatten one of the inner objects

我正在尝试合并两个数组(基于 ID)并展平其中一个对象。我看了看,找不到类似的东西。

这是我正在尝试做的事情以及我已经取得的成就:

const a = {
    someProperty: 'something',
    anotherProperty: 'somethingelse',
    listOfUsers: [
        { id: 100, a1: 'somea1', a2: 'somea2'},
        { id: 101, a1: 'somea1', a2: 'somea2'},
        { id: 102, a1: 'somea1', a2: 'somea2'},
        { id: 103, a1: 'somea1', a2: 'somea2'}
    ]

}
const b = [
    { id: 100, b1: { b2: 'someB2', b3: 'someB3'}},
    { id: 101, b1: { }}, //can be empty,
    { id: 102, b1: { b2: undefined, b3: 'someB3'}}, //b2 can be undefined
    { id: 103, b1: { b2: 'someB2', b3: 'someB3'}}
]

const aAndB = a.listOfUsers.map(item => ({
    ...item,
    b_role: { ...b.find((itemInner) => itemInner.id === item.id) }
}));

这给了我:

{
    id: 100, a1: 'somea1', a2: 'somea2',
    b_role: {b2: 'someB2', b3: 'someB3'}
}
// if nothing is found:
{
    id: 100, a1: 'somea1', a2: 'somea2',
    b_role: {}
}

但我想要:

{
    id: 100, a1: 'somea1', a2: 'somea2',
    b_role: 'someB2' // I want b.b2 but b can be empty
}
// if nothing is found:
{
    id: 100, a1: 'somea1', a2: 'somea2',
    b_role: undefined
}

我该怎么做?如果有帮助,我可以使用下划线

再见,这里是工作示例:

const a = {
    someProperty: 'something',
    anotherProperty: 'somethingelse',
    listOfUsers: [
        { id: 100, a1: 'somea1', a2: 'somea2'},
        { id: 101, a1: 'somea1', a2: 'somea2'},
        { id: 102, a1: 'somea1', a2: 'somea2'},
        { id: 103, a1: 'somea1', a2: 'somea2'}
    ]
}
const b = [
    { id: 100, b1: { b2: 'someB2', b3: 'someB3'}},
    { id: 101, b1: {} }, //can be empty,
    { id: 102, b1: { b2: undefined, b3: 'someB3'}}, //b2 can be undefined
    { id: 103, b1: { b2: 'someB2', b3: 'someB3'}}
]

let arr_result = [];
a.listOfUsers.map(a_el => {
   b.map(b_el => {
      if (b_el.id === a_el.id) {
         let result = {};
         result.id = a_el.id;
         result.a1 = a_el.a1;
         result.a2 = a_el.a2;
         result.b_role = b_el.b1.hasOwnProperty('b2') ? b_el.b1.b2 : undefined;
         arr_result.push(result);
      }
   })
})

console.log(arr_result)

这是我的代码,也许你可以用其他值测试

const a = {
  someProperty: 'something',
  anotherProperty: 'somethingelse',
  listOfUsers: [
    { id: 100, a1: 'somea1', a2: 'somea2'},
    { id: 101, a1: 'somea1', a2: 'somea2'},
    { id: 102, a1: 'somea1', a2: 'somea2'},
    { id: 103, a1: 'somea1', a2: 'somea2'}
  ]
};

const b = [
  { id: 100, b1: { b2: 'someB2', b3: 'someB3'}},
  { id: 101, b1: { }}, //can be empty,
  { id: 102, b1: { b2: undefined, b3: 'someB3'}}, //b2 can be undefined
  { id: 103, b1: { b2: 'someB2', b3: 'someB3'}}
]

const aAndB = a.listOfUsers.map(item => ({
  ...item,
  b_role: b.find((itemInner) => itemInner.id === item.id).b1.b2
}));

console.log(aAndB);

[
  { id: 100, a1: 'somea1', a2: 'somea2', b_role: 'someB2' },
  { id: 101, a1: 'somea1', a2: 'somea2', b_role: undefined },
  { id: 102, a1: 'somea1', a2: 'somea2', b_role: undefined },
  { id: 103, a1: 'somea1', a2: 'somea2', b_role: 'someB2' }
]