I could not map two array of json data as per key value using JavaScript. My code is below:
var userdata=[{'email':'[email protected]','name':'Rajj'},{'email':'[email protected]','name':'Rajesh'}];
var userdata1=[{'email':'[email protected]','address':'rasukgarh'}];
var finalArr=[];
userdata.map(item => {
userdata1.map(item1 => {
if(item.email==item1.email){
finalArr.push(Object.assign(item, item1));
}else{
finalArr.push(Object.assign(item, item1));
}
})
})
console.log('all Data',finalArr);
}
Here my requirement is if same email id is present in both array then the additional data of second array will merge with first one. If 1st array has some data and based on the email value no data is present inside second array then in hat case only first array data will push to resultant array. Here my expected output is.
finalArr=[{'email':'[email protected]','name':'Rajj','address':'rasukgarh'},{'email':'[email protected]','name':'Rajesh'}]
But in my case I could not get like this.