如何将包含多个对象的两个或多个数组合并为一个并增加其ID?

时间:2019-03-09 18:05:41

标签: javascript arrays

我有三个这样的对象数组

pd.Series(d).loc[x].tolist()
Out[68]: [[1, 6, 9], [5, 10], [1, 3, 6]]

并且我想将它们合并成这样的单个数组

  const arr1 = [{id: 1, name: 'Jay'}, {id: 2, name: 'Kay'}];
    const arr2 = [{id: 1, name: 'Pete'}, {id: 2, name: 'Liam'}];
    const arr3 = [{id: 1, name: 'Baby'}, {id: 2, name: 'Neeson'},  {id: 3, name: 'Kobe'}];

我知道我可以使用传播运算符将它们传播到单个数组中,但是当它们加入时,我还需要增加ID。我该如何实现?

7 个答案:

答案 0 :(得分:3)

根据索引重新分配ID:

 const merged = [...arr1, ...arr2, ...arr3];
 merged.forEach((el, index) => el.id = index + 1);

答案 1 :(得分:1)

将所有数组添加到单个数组中,然后在其上映射并根据需要更改索引。

const arr1 = [{id: 1, name: 'Jay'}, {id: 2, name: 'Kay'}];
const arr2 = [{id: 1, name: 'Pete'}, {id: 2, name: 'Liam'}];
const arr3 = [{id: 1, name: 'Baby'}, {id: 2, name: 'Neeson'},  {id: 3, name: 'Kobe'}];
    
let arr = [...arr1,...arr2,...arr3]

let op = arr.map((ele, index) => ({...ele, id: index+1}) )

console.log(op)

答案 2 :(得分:1)

您可以使用rest parametersArray.flat()将数组组合为单个数组,然后可以使用Array.map()用从索引生成的id更新对象:

const flatAndInc = (...args) =>
  args.flat()
  .map((o, idx) => ({
    ...o,
    id: idx + 1
  }));

const arr1 = [{id: 1, name: 'Jay'}, {id: 2, name: 'Kay'}];
const arr2 = [{id: 1, name: 'Pete'}, {id: 2, name: 'Liam'}];
const arr3 = [{id: 1, name: 'Baby'}, {id: 2, name: 'Neeson'},  {id: 3, name: 'Kobe'}];

const result = flatAndInc(arr1, arr2, arr3);

console.log(result);

  

答案 3 :(得分:1)

您可以映射新对象而无需更改给定数据。

const
    arr1 = [{id: 1, name: 'Jay'}, {id: 2, name: 'Kay'}],
    arr2 = [{id: 1, name: 'Pete'}, {id: 2, name: 'Liam'}],
    arr3 = [{id: 1, name: 'Baby'}, {id: 2, name: 'Neeson'},  {id: 3, name: 'Kobe'}],
    result = [arr1, arr2, arr3].reduce(
        (id => (r, a) => (a.forEach(({ name }) => r.push({ id: id++, name })), r))(1),
        []
    );

console.log(result);

答案 4 :(得分:1)

所有其他答案都是有效的,并且比我的答案更有效,更容易。但是,我的答案是最容易理解的答案之一。

我的代码使用Spread Syntax (...)创建大数组,但所有操作都是组合数组。然后,我使用.forEach()循环遍历数组,并重新定义ID。然后,我增加了id变量,以使ID顺序正确。

const arr1 = [{id: 1, name: 'Jay'}, {id: 2, name: 'Kay'}];
const arr2 = [{id: 1, name: 'Pete'}, {id: 2, name: 'Liam'}];
const arr3 = [{id: 1, name: 'Baby'}, {id: 2, name: 'Neeson'},  {id: 3, name: 'Kobe'}];
let id = 1;
const bigArray = [...arr1, ...arr2, ...arr3];

bigArray.forEach(e => {
  e.id = id;
  id++;
});

console.log(bigArray);

答案 5 :(得分:1)

您可以使用Array.fromspread

 const arr1 = [{id: 1, name: 'Jay'}, {id: 2, name: 'Kay'}];
 const arr2 = [{id: 1, name: 'Pete'}, {id: 2, name: 'Liam'}];
 const arr3 = [{id: 1, name: 'Baby'}, {id: 2, name: 'Neeson'},  {id: 3, name: 'Kobe'}];

 var result = Array.from([...arr1, ...arr2, ...arr3], ({name}, i)=>{ return {id: i + 1, name} });
 console.log(result);

答案 6 :(得分:0)

您可以使用reduce() map()Spread Syntax

  1. 传递要合并以起作用的所有数组的array
  2. 使用传播运算符将array放平。
  3. 然后使用reduce()将所有数组转换为一维数组。
  4. 最后使用map()并将其id属性设置为index + 1

const arr1 = [{id: 1, name: 'Jay'}, {id: 2, name: 'Kay'}];
    const arr2 = [{id: 1, name: 'Pete'}, {id: 2, name: 'Liam'}];
    const arr3 = [{id: 1, name: 'Baby'}, {id: 2, name: 'Neeson'},  {id: 3, name: 'Kobe'}];
    
    
function merge(arrays){
  return [...arrays].reduce((ac,a) => [...ac,...a],[]).map((x,i) => ({...x,id:i+1}));
}
console.log(merge([arr1,arr2,arr3]));