Reputation: 588
I want to remove an object from an ko.observableArray I have two observableArrays
self.arrayA = ko.observableArray();
self.arrayB = ko.observableArray();
then in a function I want to remove an item.
self.myRemoval = function(item){
var arrayToRemoveFrom;
if ( somelogic ) {
arrayToRemoveFrom = self.arrayA();
}
else {
arrayToRemoveFrom = self.arrayB();
}
arrayToRemoveFrom.remove(item);
}
The line "arrayToRemoveFrom.remove(item)" causes an exception, saying remove is not a function. What would be the best way to remove "item"?
Upvotes: 2
Views: 3034
Reputation: 139758
remove
is a special function of the ko.onservableArray
.
However when you write self.arrayA();
with the ()
at the end you are returning the underlaying JavaScript array which does not have a remove
function and you get the exception.
To fix your code you just need to remove the ()
:
self.myRemoval = function(item){
var arrayToRemoveFrom;
if ( somelogic ) {
arrayToRemoveFrom = self.arrayA;
}
else {
arrayToRemoveFrom = self.arrayB;
}
arrayToRemoveFrom.remove(item);
}
Upvotes: 4