Shruti Nair
Shruti Nair

Reputation: 2034

Knockout remove function on observable array not working

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

Answers (2)

Ray
Ray

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

adiga
adiga

Reputation: 35232

.remove function is available only for observableArray. But you are setting ActivityArray as an observable. Change it to an observableArray

ActivityArray = ko.observableArray([]);

Upvotes: 2

Related Questions