3

I have two arrays something like this.

array1 = [{name:'arjun', place:'mysore'},{name:'kanka', place:'bangalore'}];
array2 = [{name: 'arjun', degree:'BE'},{name:'kanka', degree:'MCA'}]

The result Should be Something like this.

array3 = [{name: 'arjun', place:'mysore', degree:'BE'}, {name:'kanka',place:'bangalore',degree:'MCA'}];

The above result Array doesn't have duplicate values now. Can anyone help please?

Arjun Sarthi
  • 109
  • 1
  • 11

6 Answers6

3

Assuming that you need to merge arrays by index and are of same length.

let array1 = [{name:'arjun', place:'mysore'},{name:'kanka', place:'bangalore'}];
let array2 = [{name: 'arjun', degree:'BE'},{name:'kanka', degree:'MCA'}]

let array3 = array1.map((o,i) => ({...o, ...array2[i]}));
console.log(array3);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
2

You can simply use a Array.map() and Object.assign()

array1 = [{name:'arjun', place:'mysore'},{name:'kanka', place:'bangalore'}];
array2 = [{name: 'arjun', degree:'BE'},{name:'kanka', degree:'MCA'}];

let result = array1.map((a)=>{
 let obj2 = array2.find((b)=> a.name === b.name);
 if(obj2)
  Object.assign(a,obj2);
 return a;
});

console.log(result);
amrender singh
  • 7,949
  • 3
  • 22
  • 28
2

I think you can use array#reduce to do something like this perhaps:

array1 = [{name:'arjun', place:'mysore'},{name:'kanka', place:'bangalore'}];
array2 = [{name: 'arjun', degree:'BE'},{name:'kanka', degree:'MCA'}]

var resultArray = array1.reduce((arr, e) => {
  arr.push(Object.assign({}, e, array2.find(a => a.name == e.name)))
  return arr;
}, [])


console.log(resultArray);

NOTE: It will also work if those arrays are of different length.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
1

use the foreach like this

let array1 = [{name:'arjun', place:'mysore'},{name:'kanka', place:'bangalore'}];
let array2 = [{name: 'arjun', degree:'BE'},{name:'kanka', degree:'MCA'}]


let array3 = [];

array1.forEach((item, index) => {
    array3.push(item)
    let degree = array2.find(e => e.name === item.name)
    if(degree){
      array3[index].degree = degree.degree
    }
})

console.log(array3)
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
1

If both arrays are of the same length then this solution will work.

array1 = [{name:'arjun', place:'mysore'},{name:'kanka', place:'bangalore'}];
array2 = [{name: 'arjun', degree:'BE'},{name:'kanka', degree:'MCA'}]

const array3 = [];
for(let i=0; i < array1.length; i++){
    array3.push(Object.assign(array1[i], array2[i]));
}
console.log(array3);

Working JSBin here.

Look here also: How can I merge properties of two JavaScript objects dynamically?

Rohit Sharma
  • 3,304
  • 2
  • 19
  • 34
1

You can make an aggregation of properties by the name with reduce and map out the aggregated data to an array:

const array1 = [{name:'arjun', place:'mysore'},{name:'kanka', place:'bangalore'}];
const array2 = [{name: 'arjun', degree:'BE'},{name:'kanka', degree:'MCA'}];

const objData = [...array1, ...array2].reduce((all, { name, place, degree }) => {

    if (!all.hasOwnProperty(name)) all[name] = { name };
    if (place) all[name].place = place;
    if (degree) all[name].degree = degree;

    return all;

}, {});

const result = Object.keys(objData).map(k => objData[k]);

console.log(result);
Leonid Pyrlia
  • 1,594
  • 2
  • 11
  • 14