Johnny Cash
Johnny Cash

Reputation: 21

Observable Array doesn't reset in Knokcout

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?

Upvotes: 0

Views: 59

Answers (1)

ic3b3rg
ic3b3rg

Reputation: 14927

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>

Upvotes: 1

Related Questions