0

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

morne
  • 4,035
  • 9
  • 50
  • 96

2 Answers2

2

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;
Emad Dehnavi
  • 3,262
  • 4
  • 19
  • 44
  • There is just one problem. The updated object is in the list of many items. The one that needs updateing is the singular item ( visItem) object. – morne Jul 27 '17 at 09:39
  • The other way around is also possible, you just need to change the last line like YOUR_OBJECT_NAME.visItem = items[getIndex] – Emad Dehnavi Jul 27 '17 at 09:48
2

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;
    }
} 
Andrew Stalker
  • 718
  • 5
  • 22