I want to throw away the data in my observablearray everytime I get data from my server.
What is the difference in functionality between
self.myArray([]);
vs
self.myArray.removeAll();
The KnockoutJS Observable removeAll() method removes all items and returns them as an array.
To clear an observableArray you can set the value equal to an empty array.
Description. The KnockoutJS Observable sort() method sorts all items in the array. By default, items are sorted in an ascending order. For sorting an array in a descending order, use reverse() method on sorted array.
From the end result point of view there is no difference between the two call, so you will end up with myArray
containing no elements.
However there is one small difference (if you don't care about the different return values):
self.myArray([]);
will replace the underlying array instance with a newly created empty array.
While the
self.myArray.removeAll();
will remove all the items from the underlying array but it will keep the array instance.
So if you have multiple ko.observableArray
using the same underlaying array you can see the difference between the two calls:
Demo JSFiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With