175
rated 0 times
[
177]
[
2]
/ answers: 1 / hits: 19708
/ 5 Years ago, mon, february 25, 2019, 12:00:00
Got a bit of a puzzle here...I want to loop through allItems
and return allItems
but replace with any newItems
that matches its id. How can I look for a match on id
and then replace it with the correct object into the array?
const allItems = [
{
'id': 1,
'category_id': 1,
'text': 'old',
},
{
'id': 2,
'category_id': 1,
'text': 'old'
}
]
const newItems = [
{
'id': 1,
'category_id': 1,
'text': 'new',
'more_info': 'abcd'
},
{
'id': 2,
'category_id': 1,
'text': 'new',
'more_info': 'abcd'
}
]
What I tried so far:
for(let i = 0; i < allItems.length; i++) {
if(newItems.indexOf(allItems[i].id) > -1){
allItems[i] = newItems
}
}
How can I get the position of the object in newItems
and then replace it into allItems
?
More From » arrays