I am trying to iterate over two array of objects and trying to push combined object in a new array. I have two arrays, allData and users. I am iterating over these array and if author from allData matches the id of users array then I am pushing data into newData array.
But I am not able to access imageURL while pushing into the newData array. Can someone point out why it is happening and what is solution
let allData = [{
"id": 5,
"message": "bnnnbnb",
"parentId": 1,
"author": 2
}];
let users = [{
"id": 1,
"name": "abc",
"imageUrl": "avatar1.jpg"
}, {
"id": 2,
"name": "def",
"imageUrl": "avatar2.jpg"
}, {
"id": 3,
"name": "qwe",
"imageUrl": "avatar3.jpg"
}];
let newData = [];
allData.map((dataItem) => {
users.map((user) => {
if(dataItem.author === user.id){
newData.push({...dataItem, user.imageUrl})
}
})
})
I am expecting below result:
let newData = [{
"id": 5,
"message": "bnnnbnb",
"parentId": 1,
"author": 2,
"imageUrl": "avatar2.jpg"
}];