如何连接两个具有相同ID的对象

时间:2019-05-10 13:49:24

标签: javascript angularjs

如何将两个对象列表合并为一个列表

 var list_1 = [
  {id: 13205, name:'Allan', firstName:'jake'},
  {id: 13210, name:'Douglas', firstName:'jordan'}
 ]

 var list_2 = {13205: [{label:'type', sections: [{label: 'position'}]}]}

我想通过整合list_2来保留列表“ list_1”,如果它们具有相同的ID,并且在子对象“ categories”中具有相同的ID:

var list_1 = [
  {id: 13205, name:'Allan', firstName:'jake', categories : [{label:'type', 
   sections: [{label: 'position'}]}] },
  {id: 13210, name:'Douglas', firstName:'jordan'} 
 ]

我用过ES5和AngularJs

如果您有解决方案?

预先感谢您的回答

5 个答案:

答案 0 :(得分:1)

您可以使用Array.find()Object.keys()list_2检索密钥,如下所示:

var list_1 = [
  {id: 13205, name:'Allan', firstName:'jake'},
  {id: 13210, name:'Douglas', firstName:'jordan'}
 ]

 var list_2 = {13205: [{label:'type', sections: [{label: 'position'}]}]}
 
 list_1.find(obj => {
  const k = Object.keys(list_2)[0];
  if(obj.id.toString() === k) {
    obj.categories = list_2[k];
    return true;
  }
  return false;
});

console.log(list_1);

如果list_2包含多个ID:

var list_1 = [
  {id: 13205, name:'Allan', firstName:'jake'},
  {id: 13210, name:'Douglas', firstName:'jordan'}
 ]

 var list_2 = {
   13205: [{label:'type', sections: [{label: 'position'}]}],
   13210: [{label:'type'}]
 }
 
 Object.keys(list_2).forEach(k => { 
   list_1.find(obj => {
     if(obj.id.toString() === k) {
        obj.categories = list_2[k];
        return true;
     }
     return false;
   })
 });

console.log(list_1);

答案 1 :(得分:0)

您可以使用map()到list_1和Object.keys()来获取密钥的值,然后将密钥与ID进行比较。

 var list_1 = [
  {id: 13205, name:'Allan', firstName:'jake'},
  {id: 13210, name:'Douglas', firstName:'jordan'}
 ]

 var list_2 = {13205: [{label:'type', sections: [{label: 'position'}]}]}
 
 const merged = list_1.map((list1, index) => {
  Object.keys(list_2).forEach(keysList2 => {
    if (list1.id == keysList2) {
      list_1[index].section = list_2[list1.id]
    }
  })
  return list1
 })
 
 console.log(merged)

答案 2 :(得分:0)

一种方法是遍历list_2中的每个键,然后检查list_1内的每个对象中是否存在该键

var list_1 = [
    {id: 13205, name:'Allan', firstName:'jake'},
    {id: 13210, name:'Douglas', firstName:'jordan'}
]

var list_2 = {13205: [{label:'type', sections: [{label: 'position'}]}]}

Object.keys(list_2).forEach(key => {
    list_1.forEach(obj, index => {
        if (obj.id == key) {
            list_1[index].categories = list_2[key]
        }
    }
})

console.log(list_1);

答案 3 :(得分:0)

只需使用地图功能。这是根据其属性混合到数组的伪代码。在此示例中,o.id是arr2属性,item.id是arr1的属性:

var arr1=[...];
var arr2=[...];

arr3 = arr1.map(function(item){ var r=item; return r.category= arr2.filter(o=>o.id==item.id);});

答案 4 :(得分:-1)

只需使用Object.keys遍历list_2键来获取list_2的所有“ id”。对于每个键,请使用array.find上的list_1查找ID与当前循环的ID相同的项的引用。假设这些是数字,我添加了一个安全的强制类型转换(+i.id+k。一元运算符+会将它们强制转换为数字)以确保比较标准正确。 / p>

之后,如果找到任何项目,只需将looped属性分配给categories属性。

但是请

注意:如果list_1或list_2 ,则find的表现可能不合适。在这种情况下,您应该首先制作一张地图并使用它,但事实并非如此。

最后一点,这里不需要angularjs。

var list_1 = [
  {id: 13205, name:'Allan', firstName:'jake'},
  {id: 13210, name:'Douglas', firstName:'jordan'}
];
var list_2 = {13205: [{label:'type', sections: [{label: 'position'}]}]};

Object.keys(list_2).forEach(k => {
   const found = list_1.find(i => +i.id === +k);
   if (found) found.categories = list_2[k];
});

console.log(list_1);