0

How to concat two lists of objects into one list

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

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

I would like to keep the list "list_1" by integrating the list_2 if they are the same id and that in a subobject "categories" like this :

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

I used ES5 and AngularJs

If you have a solution ?

Thank you in advance for your answer

Greg-A
  • 772
  • 1
  • 19
  • 41

5 Answers5

1

You can use Array.find() and Object.keys() to retrieve the key from list_2 like this:

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

If list_2 contains multiple id's:

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);
Fraction
  • 11,668
  • 5
  • 28
  • 48
0

You can use map() to the list_1 and Object.keys() to get value of keys, then compared key with 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)
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

One way is to loop over every key in list_2 and then check if that key exists in each object inside 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);
Kobe
  • 6,226
  • 1
  • 14
  • 35
0

Simply use map function. Here is pseudo-code to mix to array based on their properties. In this example o.id is arr2 property and item.id is arr1's :

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

arr3 = arr1.map(function(item){ var r=item; return r.category= arr2.filter(o=>o.id==item.id);});
nAviD
  • 2,784
  • 1
  • 33
  • 54
-1

Just acquire all the "id" of the list_2, by using Object.keys to iterate the list_2 keys. For each key, use array.find on list_1 to find the reference of the item whose id is the same as the currently looped one. Assuming those are number, I've added a safe cast (+i.id and +k. The unary operator + will cast them to a number) to ensure that the comparison criteria is correct.

After that, if any item is found, just assign the looped property to the categories property.

BEWARE though: if either list_1 or list_2 are big, the performances of find may not be suitable. In that case, you should firstly make a map and use it, but that doesn't seem to be the case.

As a final side note, angularjs is not needed here.

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);
briosheje
  • 7,356
  • 2
  • 32
  • 54