-1

I have two arrays of objects that share a property with the same name (userId), but have different lengths. Here is a simple example:

const arr1= [
    {
        userId: "1", 
        name:"Tommy", 
        hobbies:"fighting"
    }, 
    {
       userId: "16", 
       name:"Kino", 
       hobbies:"skating"
    }
];

const arr2= [
    {
        userId: "1", 
        story:"Big fight"
    }, 
    {
        userId:"16",
        story:"big momentum"
    }
];

My ideal outcome would be to have one array which combines both objects that match in property with userId (and all objects that match in property) and keeps all of the properties of both.

I´ve tried using concat and then filtering, but to no avail. Any clues or suggestion on how this can be accomplished?

sudo97
  • 904
  • 2
  • 11
  • 22
  • Does this answer your question? [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – A. Meshu Feb 11 '20 at 00:58
  • Not really. They are array of objects, not individual objects. – Edgardo Minotauro Feb 11 '20 at 01:00
  • What do you want as the output of `story` property if an user exist in `arr1` but not `arr2`? undefined? – Andus Feb 11 '20 at 01:23

3 Answers3

1

This can be a possible solution:

const arr1 = [{userId: "1", name:"Tommy", hobbies:"fighting"}, {userId: "16", name:"Kino", hobbies:"skating"}];
const arr2 = [{userId: "1", story:"Big fight"}, {userId:"16", story:"big momentum"}];

const obj = [...arr1, ...arr2].reduce((acc, el) => {
  acc[el.userId] = {...acc[el.userId], ...el};
  
  return acc;
}, {});

const result = Object.values(obj);

console.log(result);
Yonggoo Noh
  • 1,811
  • 3
  • 22
  • 37
0

I think you need to do something like:

function combByPropVal(){
  const a = [].slice.call(arguments), prop = a.shift(), val = a.shift(), o = {};
  a[0].forEach(obj=>{
    if(prop in obj && obj[prop] === val){
      o[prop] = val;
      for(let i in obj){
        if(i !== prop)o[i] = obj[i];
      }
    }
  });
  return o;
}
const arr1 = [{userId: "1", name:"Tommy", hobbies:"fighting"}, {userId: "16", name:"Kino", hobbies:"skating"}];
const arr2 = [{userId: "1", story:"Big fight"}, {userId:"16", story:"big momentum"}];
console.log(combByPropVal('userId', '1', arr1.concat(arr2)));

combByPropVal takes three arguments property, value, and an Array of Objects. I used .concat to create a new Array before passing it in.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

Let's assume you have two arrays, arr1 and arr2. Length of arr1 might be longer or equals to arr2. Difference between an object in arr2 and arr1 is the story property.

const arr1 = [
  { userId: "1", name: "Tommy", hobbies: "fighting" }, 
  { userId: "16", name: "Kino", hobbies: "skating" }, 
  { userId: "17", name: "Tom", hobbies: "Tennis" }
];
const arr2 = [
  { userId: "1", story: "Big fight" }, 
  { userId: "16", story: "big momentum" }
];
const result = arr1.map(obj1 => {
  const obj2IfExist = arr2.find(obj2 => obj2.userId === obj1.userId);
  obj1['story'] = obj2IfExist? obj2IfExist['story'] : null;
  return obj1
})

console.log(result)

That's what you got:

[
  { userId: "1", name: "Tommy", hobbies: "fighting", story: "Big fight" },
  { userId: "16", name: "Kino", hobbies: "skating", story: "big momentum" },
  { userId: "17", name: "Tom", hobbies: "Tennis", story: null }
]
Andus
  • 1,713
  • 13
  • 30