私はknockoutJSを使ってタグシステムを使ってリストシステムを作っています。アイデアは、異なるタグを持つ複数のリストを持っているし、任意のマッチを表示することであるタグ(0、1または複数のタグ)の独自のセットを持つ各効率的な方法ですべての一致を見つけ、ある配列を別の配列と比較することはできません
-
「アイテム」の中央リストがあります。例えば
: -
item 1 - tags 1,3
item 2 - tags 2,4
item 3 - tags 4
item 4 - tags NONE
とシステム、アイテムを拾うために「いいえリスト」(のための「いいえタグ」の1と1で生成され
List 1 - tags 1,2
List 2 - tags 3
プラス2余分なリストタグとリストにそれぞれ一致しないアイテムはありません)。
これはにつながる: -
List NO TAGS = item 4
List NO LIST = item 3
List 1 = item 1, item 2
List 2 = item 1
私は複数のループを実行し、各リストで開催された項目をリセットしています現時点ではと私が午前問題は、リストに項目をソートするための効率的な方法であります変更が行われるたびに
次のコードは、私が知っている醜いです、それは参照のためのより多くのですが、私は質問が下
UGLY WORKING例
に示すデータ構造にタグを比較するための最も効率的な方法とは何かあるとしko.computed(function() {
//clear all items in arrays
ko.utils.arrayForEach(self.lists(), function (list) {
list.items([]);
});
var onList; //does this item appear on a list yet
//loop through all items and then all lists to see if an item matches any tags on that list and if so add it.
ko.utils.arrayForEach(self.items(), function (item) {
onList = 0; //reset whether found - used for adding to 'No List'
ko.utils.arrayForEach(self.lists(), function (list) {
//if an item has no tasks then add to list id - 2 'No Tags' List
if (item.tags().length == 0 && list.id() == -2) {
list.items.push(item);
onList = 1; //found a list
} else {
//now for each item loop through tags and then loop through lists tags and find any matches
ko.utils.arrayFirst(item.tags(), function (tag) {
var found = 0;
ko.utils.arrayFirst(list.tags(), function (listTag) {
console.log(listTag, tag);
if (parseInt(listTag) == parseInt(tag)) {
list.items.push(item);
found = 1;
onList = 1;
//at least one tag matches, so exit
return 1;
}
});
if (found == 1) {
//at least one tag matches, so exit
return 1;
}
});
}
});
//there were no matches in any of the lists so add this to the list 'No List'
if(onList == 0 && self.lists().length > 1){
self.lists()[1].items.push(item);
}
});
});
データ構造
///4 lists - the first 2 are system generated 'catch' lists
var listData = [
{id: -2, name: "NO TAGS", tags: [
//NOT APPLICABLE AS HANDLED BY CODE
]
},
{id: -1, name: "NO LIST", tags: [
//NOT APPLICABLE AS HANDLED BY CODE
]
},
{id: 1, name: "List 1", tags: [
2
]
},
{id: 2, name: "List 2", tags: [
1, 3
]
}
];
//2 sets of items
var itemData = [
{id: 1, name: "Item 1", addedBy: 1, tags: [
1
]},
{id: 2, name: "Item 2", addedBy: 1, tags: [
2, 3
]}
];
おそらく、コードをより速く動作させる最も良い方法は、 'deferUpdates = true'を使用することです。 –
私は私のプロジェクトをKO3.4にアップグレードしました。この質問に興味を持った理由の大半は、deferUpdates私自身。今日の仕事はアプリで試してみることです。 – sifriday
詳細な返信をしていただき、ありがとうございます。遅れた更新は既に行われており、私はあなたのポイントをオンボードに取り上げ、今週末に作業を進めていきます。私はいくつかの明白なアルゴリズムが不足していたのかどうか疑問に思ったが、私はあなたのポイントで、遅く始まってスピードを遅くすることができると思う。後で違う構造で物を書き直したくない。 +1と私は受け入れますので、あなたはいくつかの点を得る:-) –