Replacing item in observableArray

The replace function accepts two parameters, the item you want to replace and the new item you want to replace it with. You are passing in the index in place of the item to replace so it doesn’t work.

The replace call should be:

self.locations.replace(oldLocation, new location(value));

On a side note, you shouldn’t need the valueHasMutated() call there, it will get invoked by the replace() call.

Side note, many of the native Array functions are available for observable arrays. They are forwarded to the underlying array value triggering notifications of mutations as needed. These include:

pop, push, reverse, shift, sort, splice, unshift, slice (readonly).

Knockout provides these additional methods which should be documented here (currently v3.5.1):

remove, removeAll, destroy, destroyAll, indexOf, replace, sorted, reversed

Leave a Comment