将两个数组合并为一个

时间:2019-07-08 14:19:12

标签: javascript

我希望将这两个数组合并为一个数组。我希望对所有相同的ID信息进行过滤,以使其仅出现一次,从而使其成为姓名,年龄,职业和地址的简单列表。

我尝试过使用连接,过滤器来简单地整理信息,但是我似乎无法获得正确的答案。

var a = [{
  id: 'aBcDeFgH',
  firstName: 'Juan',
  lastName: 'Doe',
  age: 32
 },
{
  id: 'zYxWvUt',
  firstName: 'Alex',
  lastName: 'Smith',
  age: 24
}]


var b = [{
  id: 'aBcDeFgH',
  occupation: 'architect',
  address: {
    street: '123 Main St',
    city: 'CityTown',
    Country: 'USA'
  }
},
{
  id: 'zYxWvUt',
  occupation: 'receptionist',
  address: {
    street: '555 Ocean Ave',
    city: 'Beach City',
    Country: 'USA'
  }
}]

在concat之后,我总是只得到一个列表,但是我找不到如何过滤相同信息的信息。

6 个答案:

答案 0 :(得分:3)

您可以从第一个数组hive> CREATE view sta_v_clean_phrase_longtext (system, `function`, loglockmod, extinfo , primarykey, foreignkey, flgprimkey, flgfrgnkey, ref_recn, text_line, copied) AS SELECT system, `function`, loglockmod, extinfo, primarykey, foreignkey, flgprimkey, flgfrgnkey, ref_recn, Concat_ws('', Collect_list(text_line))AS TEXT_LINE, copied FROM (SELECT system, `function`, loglockmod, extinfo, primarykey, foreignkey, flgprimkey, flgfrgnkey, ref_recn, text_line, copied FROM phrase_longtext_orc_orc WHERE primarykey IS NOT NULL GROUP BY system, `function`, loglockmod, extinfo, primarykey, foreignkey, flgprimkey, flgfrgnkey, ref_recn, text_line, copied) t1 GROUP BY system, `function`, loglockmod, extinfo, primarykey, foreignkey, flgprimkey, flgfrgnkey, ref_recn, text_line, copied 中创建一个对象,整个键将成为每个对象的a。然后在id上使用map()并返回具有所有道具的物体。

b

答案 1 :(得分:3)

听起来像您需要将每个数组的每个项目合并在一起-并且它们的顺序相同,在这种情况下,您可以这样做:

const newList = []

a.forEach((item, index) => {
  newList.push({
    ...item,
    ...b[index]
  })
})

console.log(newList)

答案 2 :(得分:1)

以下内容将以与a相同的顺序重构数组,但是该函数并不依赖于b处于相同的顺序。

var a = [{id:"aBcDeFgH",firstName:"Juan",lastName:"Doe",age:32},{id:"zYxWvUt",firstName:"Alex",lastName:"Smith",age:24}],
    b = [{id:"aBcDeFgH",occupation:"architect",address:{street:"123 Main St",city:"CityTown",Country:"USA"}},{id:"zYxWvUt",occupation:"receptionist",address:{street:"555 Ocean Ave",city:"Beach City",Country:"USA"}}];

let res = a.reduce((a,c) => {a.push({...c, ...b.find(v => v.id == c.id)}); return a;},[])

console.log(res)

作为一种性能更高的解决方案解决方案,不是为每次查询都使用find,而是将类似地图的对象用于第二个数组,因此我们只需从中插入通过O(1)查找得到的结果。

所以我们现在有了O(n),而不是O(n²):

var a = [{id:"aBcDeFgH",firstName:"Juan",lastName:"Doe",age:32},{id:"zYxWvUt",firstName:"Alex",lastName:"Smith",age:24}],
    b = [{id:"aBcDeFgH",occupation:"architect",address:{street:"123 Main St",city:"CityTown",Country:"USA"}},{id:"zYxWvUt",occupation:"receptionist",address:{street:"555 Ocean Ave",city:"Beach City",Country:"USA"}}];

let tmp = b.reduce((a,c) => {a[c.id] = c; return a},{}),
    res = a.reduce((a,c) => {a.push({...c, ...tmp[c.id]}); return a;},[]);

console.log(res)

答案 3 :(得分:0)

尝试一下:

 var a = [{
  id: 'aBcDeFgH',
  firstName: 'Juan',
  lastName: 'Doe',
  age: 32
 },
{
  id: 'zYxWvUt',
  firstName: 'Alex',
  lastName: 'Smith',
  age: 24
}]

var b = [{
  id: 'aBcDeFgH',
  occupation: 'architect',
  address: {
    street: '123 Main St',
    city: 'CityTown',
    Country: 'USA'
  }
},
{
  id: 'zYxWvUt',
  occupation: 'receptionist',
  address: {
    street: '555 Ocean Ave',
    city: 'Beach City',
    Country: 'USA'
  }
}];


const newA =   a.reduce((acc, ele) => (acc[ele.id] = ele, ele),{});
const result  = b.map(ele=> ({...newA[ele.id],...ele}));

console.log(result);

答案 4 :(得分:0)

如果id是比较关键字,

const concatListById = (base, target) => base.reduce((acc, c) => {
    const matched = target.find(e => e.id === c.id);
    let el = c;
    if (matched) el = { ...matched, ...c };
    acc.push(el);
    return acc;
}, []);

console.log(concatListById(a, b));

如果假设每个数组的id序列相同,

const justMergeArray = (base, target) => 
  base.map((e, idx) => ({ ...e, ...target[idx] }));

console.log(justMergeArray(a, b));

答案 5 :(得分:0)

您可以使用reduce并比较id match是否只是推入相同的对象并将其添加到array

var a = [{
    id: 'aBcDeFgH',
    firstName: 'Juan',
    lastName: 'Doe',
    age: 32
  },
  {
    id: 'zYxWvUt',
    firstName: 'Alex',
    lastName: 'Smith',
    age: 24
  }
]


var b = [{
    id: 'aBcDeFgH',
    occupation: 'architect',
    address: {
      street: '123 Main St',
      city: 'CityTown',
      Country: 'USA'
    }
  },
  {
    id: 'zYxWvUt',
    occupation: 'receptionist',
    address: {
      street: '555 Ocean Ave',
      city: 'Beach City',
      Country: 'USA'
    }
  }
]

const res = a.reduce((all, acc, index) => {
  if (acc.id === b[index].id) {
    all.push({
      ...acc,
      ...b[index]
    });
  }
  return all;
}, []);

console.log(res);