如何从可观察数组中的可观察数组中删除一个项目
How to remove an item from an observable array which is inside an observable array
如果 ko.observableArray 中有一个 ko.observableArray 我如何从中删除项目甚至 select 数组
通常,您会用更容易识别的东西包裹数组。喜欢:
this.boxes = ko.observableArray([
{ id: 1, items: ko.observableArray([1, 2, 3]) },
{ id: 2, items: ko.observableArray([4, 5]) }
]);
如果您不想,最好在包装之前保存对数组的引用:
const firstBox = ko.observableArray([1, 2, 3]);
const secondBox = ko.observableArray([4, 5]);
this.boxes = ko.observableArray([firstBox, secondBox]);
firstBox.remove(2);
请注意,此删除 不会 触发 boxes
上的更新。
您还可以查找包含要删除的项目的数组。一旦有多个匹配项,您就必须决定要做什么...
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);
如果 ko.observableArray 中有一个 ko.observableArray 我如何从中删除项目甚至 select 数组
通常,您会用更容易识别的东西包裹数组。喜欢:
this.boxes = ko.observableArray([
{ id: 1, items: ko.observableArray([1, 2, 3]) },
{ id: 2, items: ko.observableArray([4, 5]) }
]);
如果您不想,最好在包装之前保存对数组的引用:
const firstBox = ko.observableArray([1, 2, 3]);
const secondBox = ko.observableArray([4, 5]);
this.boxes = ko.observableArray([firstBox, secondBox]);
firstBox.remove(2);
请注意,此删除 不会 触发 boxes
上的更新。
您还可以查找包含要删除的项目的数组。一旦有多个匹配项,您就必须决定要做什么...
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);