0

enter image description hereI have an array that stores distinct Ids, but the user have the ability to edit these items.

I use this function to only add distinct values

var match = ko.utils.arrayFirst(addKo.SpecialCharges.AddedSpecialChargesList(), function(itemToAdd) {
    return item.idx_Specials() === itemToAdd.idx_Specials();
});

if (!match) {
    addKo.SpecialCharges.AddedSpecialChargesList.push(new vmFormElementSpecialCharges(item));
}

What I want is to edit an already existing item in this array without getting an error saying that the same item already exists!

Edit 1: Added Screenshot for better understanding

Mohamed Said
  • 524
  • 7
  • 27
  • Take a look - http://stackoverflow.com/questions/15440810/replacing-item-in-observablearray.. Don't add a new item, just replace the older one with the new item. – gkb Dec 16 '16 at 04:58
  • @gkb the issue is not with modifying it, I want to validate that it's not adding an already existing item as well when being edited – Mohamed Said Dec 16 '16 at 12:15
  • so you want to validate before adding that an object already does not exist? Also, can you provide some sample data that might be in the `addKo.SpecialCharges.AddedSpecialChargesList` array? – gkb Dec 16 '16 at 12:16
  • @gkb the Scenario is as follows: I have an array that contains items the user added, the user can't add same item twice the item contains id and other attributes, and can edit this specific item, what happens is that when he's editing an item and saves it says that it already exists because the ID already exists when saving, I don't want to "replace" because this may cause duplicates – Mohamed Said Dec 16 '16 at 12:17
  • so is your code not working? It seems to be okay at first glance. Do you get `the same item already exists` error with your current code?? – gkb Dec 16 '16 at 12:23
  • @gkb yep! ... It works when I'm adding an item for the first time, but when I decide to edit this item it says it exists as it compares itself with the function above – Mohamed Said Dec 16 '16 at 12:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130782/discussion-between-gkb-and-mohamed-hesham). – gkb Dec 16 '16 at 12:26

0 Answers0