Observable Array doesn't reset in Knokcout

57 Views Asked by At

I have an observable array as:

self.myArray = ko.observableArray([1234]);

I am trying to clear an observable array as two different ways:

self.myArray([]) // Step X
self.myArray.removeAll() // Step X

But the value isn't emptied, and then I have to perform:

self.myArray = ko.observableArray([]); // Step Y

Is step X's same as step Y?

1

There are 1 best solutions below

0
ic3b3rg On

Are you sure your array is not empty after removeAll()?

The test would be self.myArray().length === 0:

const test = ko.observableArray([1,2,3]);

console.log(test().length);

test.removeAll();

console.log(test().length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>