如何根据现有集合从挖空集合中删除项目的子集

时间:2014-08-05 19:46:00

标签: knockout.js

是Knockout.js的新手。

function UniqueCustomerViewModel(dataFromServer){
    self.customerMasterList = ko.observableArray(@Html.Raw(Json.Encode(ViewBag.CustomerList)));
    self.chosenCustomerList = ko.observableArray(dataFromServer.Customer.ChosenCustomers);
    ko.utils.arrayForEach(chosenCustomerList(), function (customerRow) {
        self.customerMasterList.remove(customerRow);
    });
    // ...
}

获取错误:0x800a138a - Microsoft JScript运行时错误:行ko.utils.arrayForEach上预期的函数

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我想我发现了问题,你self错过了chosenCustomerList

...
ko.utils.arrayForEach(self.chosenCustomerList(), function (customerRow) {
    self.customerMasterList.remove(customerRow);
});
...

至于删除元素 - 如果它不是普通类型,则可能必须首先在self.customerMasterList()中找到元素,然后将其删除。像这样:

...
ko.utils.arrayForEach(self.chosenCustomerList(), function (customerRow) {
    var customer = ko.utils.arrayFirst(self.customerMasterList(), function(item) {
        return item.id === customerRow.id; // or something like thatt
    });
    if (customer)
        self.customerMasterList.remove(customer);
});
...