Reputation: 100
If there is a ko.observableArray inside a ko.observableArray how would I remove items from it or even select the array
Upvotes: 0
Views: 83
Reputation: 23382
Usually, you wrap the array with something that makes it easier to recognize. Like:
this.boxes = ko.observableArray([
{ id: 1, items: ko.observableArray([1, 2, 3]) },
{ id: 2, items: ko.observableArray([4, 5]) }
]);
If you don't want to, it's probably best to either save a reference to the array before wrapping it:
const firstBox = ko.observableArray([1, 2, 3]);
const secondBox = ko.observableArray([4, 5]);
this.boxes = ko.observableArray([firstBox, secondBox]);
firstBox.remove(2);
Note that this removal will not trigger an update on boxes
.
You can also look for an array containing the item you want to remove. Once there are multiple matches, you'll have to decide what to do...
this.boxes = ko.observableArray([
ko.observableArray([1, 2, 3]),
ko.observableArray([4, 5])
]);
const remove = x => {
const inBoxes = this.boxes().filter(box => box().includes(x));
if (inBoxes.length !== 1) // What to do here?
else inBoxes[0].remove(x);
};
remove(2);
Upvotes: 1