0

My apologies if my question doesn't make sense, and I will try to explain it better...I have an on object and in this object I have an array of objects. I am trying to update one of the found objects in the array of objects, I update the found object but it doesn't update the original object in the array, right now I have this

let vendeeCatalogs = workingVendorImplementorInfo.SVendorCatalogImplementorInfo; // This the array of objects 

if (vendeeCatalogs.length > 0) {
    for (let i = 0; i < vendeeCatalogs.length; i++) {
        foundCatalog = workingVendorImplementorInfo.SVendorCatalogImplementorInfo.find(function (x) { return x.CatalogID == vendeeCatalogs[i].CatalogID });
        if (foundCatalog) {
            foundCatalog.CatalogGenerationGUID = vendeeCatalogs[i].CatalogGenerationGUID;
            foundCatalog.BeginEffectiveDate = vendeeCatalogs[i].BeginEffectiveDate;
            foundCatalog.EndEffectiveDate = vendeeCatalogs[i].EndEffectiveDate;
            foundCatalog.Multiplier = vendeeCatalogs[i].Multiplier;
            foundCatalog.Discount = vendeeCatalogs[i].Discount;
            foundCatalog.UOMPrecisionTypeID = vendeeCatalogs[i].UOMPrecisionTypeID;
            foundCatalog.IsSelected = vendeeCatalogs[i].IsSelected;
        }
    }
}

I can see that this is wrong because all it does is updates the foundCatalog, and not the original object that was found. So how do I find the object and update that object so that the changes are saved in the workingVendorImplementorInfo.SVendorCatalogImplementorInfo?

3
  • Because i is used in a callback, it will always be vendeeCatalogs.length Commented Jan 10, 2020 at 19:22
  • @Shinigami That's not true, since the code uses let i as the declaration. let uses block scope, so the value of i will be correct on each iteration. (Not to mention, the callback here is synchronous, so it wouldn't matter either way, it'll still work.) Commented Jan 10, 2020 at 19:25
  • Have you tried it with (x) => x.CatalogID == vendeeCatalogs[i].CatalogID Commented Jan 10, 2020 at 19:29

1 Answer 1

2

Why are you not using findIndex() in place of find ?

If you have the index you can then just do something like

vendeeCatalogs[x].CatalogGenerationGUID = ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.