morne
morne

Reputation: 4189

search and replace object to update

Im trying to find a "item" object which has updated values, based on another "item" object (visItem) in this case.

then I want to overwrite the "visItem" with the found and updated "item"

I have tried to base it on something like the link below, but im not sure how to implement it in my scenario. Searching and then overwriting the outdated object.

JS search in object values

Here is some pictures to set the scene.

not updated object enter image description here

update section containing the wanted item enter image description here

Upvotes: 0

Views: 314

Answers (2)

Andrew Stalker
Andrew Stalker

Reputation: 767

So you want to iterate through the items array, and find an object where the grpid and id match?

if so, you could do a simple search:

for(i=0; i<items.length; i++) {
    if(items[i].grpid === visItem.grpid && items[i].id === visItem.id) {
        //Match found
        visItem = items[i]; //Overwrites visItem with the found item
        break;
    }
} 

Upvotes: 2

Emad Dehnavi
Emad Dehnavi

Reputation: 3451

Check the FindIndex , with using this you can find index in the array of the object and replace it with your data.

var item = {YOUR UPDATED ITEM}
var items = [YOUR ARRAY OF ITEMS];

var getIndex= items.findIndex(x => x.id == item.id);
items[getIndex] = item;

Upvotes: 2

Related Questions