简体   繁体   中英

Can not remove object from knockout observableArray

I have troubles with removing object from my observable array.

It's declaration:

self.points = ko.observableArray([]);

It's holding:

self.map.points.push(new google.maps.LatLng(a, b));

I'm trying to remove some elements from this array using this code:

self.points.remove(val.internalMarker.position)

Where val.internalMarker.position hold the same LatLong object initialized with the same values as that point.

But for unknown reason remove function leaves array untouched.

Here You can check my full code on JSFiddle

Have you tried this?

self.points.remove(function(pos) {
    return pos.lat() == val.internalMarker.postition.lat()
        && pos.lng() == val.internalMarker.postition.lng();
})

Knockout's observableArray.remove overload that takes a single parameter will not work unless the object you want to remove is the exact same object. In your question, it sounds like the LatLng is not the same exact object, just that it contains the same exact lat & lng values. In these cases, you have to pass a function that compares the values you are interested in between the 2 object instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM