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?

2
  • 2
    What have you tried so far? Commented Jul 11, 2018 at 10:23
  • Do arrays have the same length? Same order? Commented Jul 11, 2018 at 10:24

6 Answers 6

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

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

Comments

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

Comments

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.

Comments

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)

Comments

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?

Comments

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

Comments

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.