简体   繁体   English

如何从可观察对象的observableArray中移除项目

[英]How to remove an item from an observableArray of observable objects

I have a very large observable array, and each item in that array is an observable. 我有一个非常大的可观察数组,该数组中的每个项目都是可观察的。 I'm trying to remove an item from the observable array, but since each item in the array is observable myArray.remove(item) doesn't seem to work. 我试图从可观察的数组中删除一个项目,但是由于数组中的每个项目都是可观察的, myArray.remove(item)似乎不起作用。 Is there a way to remove the item without converting the whole array to non-observables, removing the item, and then repopulating the observable array? 有没有一种方法可以删除项目,而无需将整个数组转换为不可观察的对象,而是先删除项目,然后重新填充可观察的数组? It feels like I'm missing something simple/obvious... 感觉像我缺少简单/明显的东西...

Try using myArray.remove(item()) 尝试使用myArray.remove(item())


I had the same problem and this is my solution 我有同样的问题,这是我的解决方案

self.images() is an observablearray of observable image self.images()是可观察图像的observablearray

So you can loop on your myArray to find the item and than remove it. 因此,您可以循环使用myArray来查找项目,然后将其删除。

$.each(self.images(), function (index, value) {
                if (value.id_image() == id_image) {
                    self.images.remove(value);
                    return false; ;
                }
            });

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

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