Reputation: 2034
In my knockout application I'm trying to remove an element using remove
method.
But i'm getting the error
.remove is not a function
Below is the code
ActivityArray = ko.observable([]);
ActivityArray(result.Regular); //Result of an api call
ActivityArray.remove(function (element) { return element.Name === 'abc'; })
Not able to understand why it's not working. I have used remove
in other places and every other place its working fine.
Please Guide
Thanks
Upvotes: 1
Views: 373
Reputation: 3959
You have to use an observableArray
(https://knockoutjs.com/documentation/observableArrays.html), not an observable
.
var viewmodel = function(){
var self = this;
var ActivityArray = ko.observableArray([]);
var result = {
"Regular":[
{"Name": "abc"},
{"Name": "xyz"}
]
};
ActivityArray(result.Regular); //Result of an api call
console.log("Before remove: ", ActivityArray());
ActivityArray.remove(function (element) { return element.Name === 'abc'; });
console.log("After remove: ", ActivityArray());
};
ko.applyBindings(new viewmodel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
Upvotes: 0