Merge two array of objects based on matching properties

Akshay

I've 2 arrays with partial information and I wish to merge those arrays with all the information into one array.

Array 1 :

const arr1 = [
    {
        name: 'Rohan',
        surname: 'Mehra',
        age: '15',
        date: "2021-01-19",
        location: 'Goa'
    },
    {
        name: 'Aman',
        surname: 'Kohli',
        age: '14',
        date: "2021-01-19",
        location: 'Kolkata'
    },
    {
        name: 'Sam',
        surname: 'Sharma',
        age: '16',
        date: "2021-01-21",
        location: 'Mumbai'
    }
]

Array 2 :

const arr2 = [
    {
        rollNo: 1,
        marks: 100,
        name: 'Rohan',
        date: "2021-01-19",
    },
    {
        rollNo: 2,
        marks: 90,
        surname: 'Kohli',
        date: "2021-01-19",
    },
    {
        rollNo: 3,
        marks: 70,
        date: "2021-01-21",
        ExamCenter: {
            place: 'Mumbai'
        }
    }
]

I want to get a final array with the properties from both arrays. The Object keys sometimes change and I wanted to match the key with the other common key and merge them. But I am not able to proceed with the solution. Here is the result array I wish to get.

const final = [
    {
        name: 'Rohan',
        surname: 'Mehra',
        age: '15',
        date: "2021-01-19",
        location: 'Goa',
        rollNo: 1,
        marks: 100,
    },
    {
        name: 'Aman',
        surname: 'Kohli',
        age: '14',
        date: "2021-01-19",
        location: 'Kolkata',
        rollNo: 2,
        marks: 90,
    },
    {
        name: 'Sam',
        surname: 'Sharma',
        age: '16',
        date: "2021-01-21",
        location: 'Mumbai',
        rollNo: 3,
        marks: 70,
    }
]

I'm trying with nested map loops but not able to proceed

const final = arr1.map((item,index) => {
    arr2.map((innerItem, i) => {
        if(item[Object.keys(innerItem)][index] === innerItem[Object.keys(innerItem)][0]){
            console.log(item);
        }
    })
})
sabbir.alam

There is a mistake in your arr2. The surname for 2nd item should be kohli instead of kolhi. Anyway, You can do the following to merge two array based on dynamic matching attribute. What we are doing here is,

For each item of arr1 we are finding the keys using Object.keys method and checking which object from arr2 has maximum matching object with the item of arr1. Then we merge the two item together.

arr1 = [
    {
        name: 'Rohan',
        surname: 'Mehra',
        age: '15',
        date: "2021-01-19",
        location: 'Goa'
    },
    {
        name: 'Aman',
        surname: 'Kohli',
        age: '14',
        date: "2021-01-19",
        location: 'Kolkata'
    },
    {
        name: 'Sam',
        surname: 'Sharma',
        age: '16',
        date: "2021-01-21",
        location: 'Mumbai'
    }
]

arr2 = [
    {
        rollNo: 1,
        marks: 100,
        name: 'Rohan',
        date: "2021-01-19",
    },
    {
        rollNo: 2,
        marks: 90,
        surname: 'Kohli',
        date: "2021-01-19",
    },
    {
        rollNo: 3,
        marks: 70,
        date: "2021-01-21",
        ExamCenter: {
            place: 'Mumbai'
        }
    }
]

res = arr1.map(item => {
  keys1 = Object.keys(item);
  let max = 0;
  const temp = arr2.reduce((prev, item2) => {
    maxTemp = keys1.filter(key => item[key] === item2[key]).length;

    if(maxTemp > max) {
      max = maxTemp;
      prev = item2;
    }
    return prev;
  }, {})
  
  if(temp) {
    return {...item, ...temp}
  }
});

console.log(res);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Merge two array of objects based on a key

Sort array of objects by two properties

Merge two objects but only existing properties

Merge objects in array based on property

How to merge two matching objects from different array into one object?

merge two array objects

Loop through JSON array of objects and get the properties based on the matching IDs from objects

Merge two objects without removing the original properties

How to merge two lists of objects based on properties and merge duplicates into new object

merge two files based on partial matching

Merge two JavaScript Objects and conflicting properties

merge two javascripts array objects by adding index based on count

Merge two n number of array of objects based on a key

Merge methods of two objects with same properties into array for each property

PHP: Merge two array of objects

Merge two array of objects

how to group an array of objects based on two properties

Removing objects from array based on two properties

merge to objects array matching id

Merge two unidentical array objects with matching same key in Typescript

Merge two array of objects based on a key value compare

Merge two array of objects by ID?

merge two array with common objects

Merge properties of a list to another based on properties objects

Use associative array to merge two objects based on ID

Merge objects in array of objects based on id

Efficient way to merge two array of objects in javascript based on similarities?

Merge nested objects in javascript, and append matching properties in array

How to merge two array of objects based on same key and remove others?