Chris
Chris

Reputation: 3129

How to update an object from an array of objects that is in an object?

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?

Upvotes: 0

Views: 49

Answers (1)

Elliot Kersal&#233;
Elliot Kersal&#233;

Reputation: 36

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 = ...

Upvotes: 2

Related Questions