2

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);
        }
    })
})
3
  • Is there any common attribute within both array? Commented Feb 23, 2021 at 7:43
  • Hi @Yadab, I think date can be a common attribute. Commented Feb 23, 2021 at 7:46
  • It looks like you want to merge the elements of both arrays based on that they share the same position in their respective original ones, Is that the case? Commented Feb 23, 2021 at 8:15

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

This may help you

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

1 Comment

nice answer, please consider leaving the original arr1 unchanged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.