简体   繁体   English

无法从删除的ObservableArray中移除对象

[英]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. 其中val.internalMarker.position包含使用与该点相同的值初始化的同一val.internalMarker.position对象。

But for unknown reason remove function leaves array untouched. 但是由于未知原因,删除功能使数组保持不变。

Here You can check my full code on JSFiddle 在这里您可以在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. 除非您要删除的对象是完全相同的对象,否则采用单个参数的淘汰赛的observableArray.remove重载将不起作用。 In your question, it sounds like the LatLng is not the same exact object, just that it contains the same exact lat & lng values. 在您的问题中,听起来LatLng不是完全相同的对象,只是它包含相同的确切lat&lng值。 In these cases, you have to pass a function that compares the values you are interested in between the 2 object instances. 在这些情况下,您必须传递一个函数来比较两个对象实例之间您感兴趣的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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