Akshay
Akshay

Reputation: 391

Merge two array of objects based on matching properties

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

Upvotes: 2

Views: 2639

Answers (3)

TheCrown
TheCrown

Reputation: 39

This may help you

    const arr3 = arr1.map((value, index) => {
      return Object.assign(value, arr2[index])
    })

Upvotes: 0

Md Sabbir Alam
Md Sabbir Alam

Reputation: 5054

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

Upvotes: 1

Yadab
Yadab

Reputation: 1883

You can do something like this to merge two arrays.

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

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

arr2.forEach((item) => {
  const array1Item = arr1.find(({ date }) => date === item.date);
  
  if (array1Item) {
    newArray.push({
      ...item,
      ...array1Item,
    })
  }
})

console.log(newArray);

Upvotes: 1

Related Questions