简体   繁体   中英

Find intersection between two arrays of objects of different length

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?

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

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.

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 }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM