gpt4 book ai didi

javascript - remove 不是可观察数组中的函数

转载 作者:行者123 更新时间:2023-11-28 20:01:26 25 4
gpt4 key购买 nike

我在knockoutjs中创建了一个observableArray,我想从该数组中删除项目并尝试如下

self.work_days = ko.observableArray();

self.work_days().push(new WorkDayVM({}, date))
//WorkDayVM is a view model and date is date object this works fine and values are //assigned well in array.

但是当尝试删除时

self.work_days.remove(days_to_remove[i]);
//also tried as self.work_days().remove(days_to_remove[i]);

我也尝试过

for(var i = 0; i < days_to_remove.length; i++){
self.work_days.remove(function(item){
return item.work_days.day_string == days_to_remove[i].day_string;
});
}

但我总是遇到同样的错误

self.work_days().remove is not a function

最佳答案

self.work_days().remove is not a function

尝试直接在 observableArray 上调用 .remove() (和 .push()),而不是尝试在数组值上调用它。self.work_days.remove 代替 self.work_days().remove

http://jsfiddle.net/NNU77/

关于javascript - remove 不是可观察数组中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21602824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com