簡體   English   中英

如何從可觀察數組內部的可觀察數組中刪除項目

[英]How to remove an item from an observable array which is inside an observable array

如果在ko.observableArray內部有一個ko.observableArray,我如何從中刪除項目,甚至選擇數組

通常,您使用易於識別的東西包裝數組。 喜歡:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM