console.log(array)返回空,但console.log(array.length)不返回0(console.log(array) returns empty, but console.log(array.length) doesn't return 0)
我有以下javascript
var filtered = []; $('#footballCheck').on('change', function() { if ($('#footballCheck').is(':checked')) { for (var i = 0; i < fa_names.length; i++) { if (fa_names[i]["facility_activity"].toLowerCase().indexOf("football") >= 0) { filtered.push(fa_names[i]); } } filtered.sort(SortByName); $('#mCSB_1_container').empty(".facility-name"); for (var k = 0; k < filtered.length; k++) { $('#mCSB_1_container').append('<div class="row facility-name">\ <button class="btn btn-default btn-fill btn-menu" date-name="' + filtered[k]["facility_name"] + '">' + filtered[k]["facility_name"] + '</button></div>'); $('#facilities-body').mCustomScrollbar("update"); } } else { for (var j = 0; j < filtered.length; j++) { if (filtered[j]["facility_activity"].toLowerCase().indexOf("football") >= 0) { delete filtered[j]; } } console.log(filtered); console.log(filtered.length); if (filtered.length > 0) { filtered.sort(SortByName); $('#mCSB_1_container').empty(".facility-name"); for (var k = 0; k < filtered.length; k++) { $('#mCSB_1_container').append('<div class="row facility-name">\ <button class="btn btn-default btn-fill btn-menu" date-name="' + filtered[k]["facility_name"] + '">' + filtered[k]["facility_name"] + '</button></div>'); $('#facilities-body').mCustomScrollbar("update"); } } else { $('#mCSB_1_container').empty(".facility-name"); for (k = 0; k < fa_names.length; k++) { $('#mCSB_1_container').append('<div class="row facility-name">\ <button class="btn btn-default btn-fill btn-menu" date-name="' + fa_names[k]["facility_name"] + '">' + fa_names[k]["facility_name"] + '</button></div>'); $('#facilities-body').mCustomScrollbar("update"); } } } });
应该做以下事情:
- 如果选中该复选框,则浏览另一个数组并将键值“facility_activity”包含“football”的所有对象推送到已
filtered
数组- 如果未选中,则从
filtered
数组中删除其键值“facility_activity”包含“football”的所有对象- 如果未选中并且其长度> 0(意味着仍有过滤的对象)则打印它们
- 如果取消选中并且
filtered
数组为空,则追加父数组中的所有项(未过滤的项)这是小提琴 。
背景:复选框充当搜索的
filtered
器,filtered
数组存储那些过滤后的值(除了那个小提琴之外的其他地方,filtered
数组还没有更改)。我的问题是
console.log(filtered)
返回[]
,console.log(filtered.length)
不返回0
,它应该。 为什么会发生这种情况,我该如何解决?I have the following javascript
var filtered = []; $('#footballCheck').on('change', function() { if ($('#footballCheck').is(':checked')) { for (var i = 0; i < fa_names.length; i++) { if (fa_names[i]["facility_activity"].toLowerCase().indexOf("football") >= 0) { filtered.push(fa_names[i]); } } filtered.sort(SortByName); $('#mCSB_1_container').empty(".facility-name"); for (var k = 0; k < filtered.length; k++) { $('#mCSB_1_container').append('<div class="row facility-name">\ <button class="btn btn-default btn-fill btn-menu" date-name="' + filtered[k]["facility_name"] + '">' + filtered[k]["facility_name"] + '</button></div>'); $('#facilities-body').mCustomScrollbar("update"); } } else { for (var j = 0; j < filtered.length; j++) { if (filtered[j]["facility_activity"].toLowerCase().indexOf("football") >= 0) { delete filtered[j]; } } console.log(filtered); console.log(filtered.length); if (filtered.length > 0) { filtered.sort(SortByName); $('#mCSB_1_container').empty(".facility-name"); for (var k = 0; k < filtered.length; k++) { $('#mCSB_1_container').append('<div class="row facility-name">\ <button class="btn btn-default btn-fill btn-menu" date-name="' + filtered[k]["facility_name"] + '">' + filtered[k]["facility_name"] + '</button></div>'); $('#facilities-body').mCustomScrollbar("update"); } } else { $('#mCSB_1_container').empty(".facility-name"); for (k = 0; k < fa_names.length; k++) { $('#mCSB_1_container').append('<div class="row facility-name">\ <button class="btn btn-default btn-fill btn-menu" date-name="' + fa_names[k]["facility_name"] + '">' + fa_names[k]["facility_name"] + '</button></div>'); $('#facilities-body').mCustomScrollbar("update"); } } } });
that should do the following:
- If the checkbox is checked, go through another array and push all objects whose key value "facility_activity" contains "football" to the
filtered
array- If it is unchecked, then remove all objects from
filtered
array whose key value "facility_activity" contains "football"- If it is unchecked and its length is > 0 (meaning that there are still filtered objects) print those
- If it is unchecked and the
filtered
array is empty, then append all items from the parent array (the one that is not filtered)Here is the fiddle.
Background: The checkboxes act as filters for a search and the
filtered
array stores those filtered values (in no other place other than what is in that fiddle thefiltered
array is changed yet).My problem is that
console.log(filtered)
returns[]
andconsole.log(filtered.length)
does NOT return0
and it should. Why does this happen and how can I fix it?
原文:https://stackoverflow.com/questions/36825511
最满意答案
您可以通过将一系列序列转换线程化来完成此操作。
(->> data (group-by #(->> % key (take 2))) vals (map (comp first first (partial sort-by (comp - val)))) (map (juxt #(subvec % 0 2) #(% 2))) (into {})) ;{[0 1] "a", [1 1] "a"}
......在哪里
(def data {[0 1 "a"] 2, [0 1 "b"] 1, [1 1 "a"] 1})
您逐行构建解决方案。 我建议你跟随建筑的脚步,从...开始
(->> data (group-by #(->> % key (take 2))) ;{(0 1) [[[0 1 "a"] 2] [[0 1 "b"] 1]], (1 1) [[[1 1 "a"] 1]]}
堆叠(懒惰)序列的层可以运行得相当慢,但是Clojure 1.7中提供的换能器将允许您用这个成语编写更快的代码,如这个优秀的答案所示 。
You can do this by threading together a series of sequence transformations.
(->> data (group-by #(->> % key (take 2))) vals (map (comp first first (partial sort-by (comp - val)))) (map (juxt #(subvec % 0 2) #(% 2))) (into {})) ;{[0 1] "a", [1 1] "a"}
... where
(def data {[0 1 "a"] 2, [0 1 "b"] 1, [1 1 "a"] 1})
You build up the solution line by line. I recommend you follow in the footsteps of the construction, starting with ...
(->> data (group-by #(->> % key (take 2))) ;{(0 1) [[[0 1 "a"] 2] [[0 1 "b"] 1]], (1 1) [[[1 1 "a"] 1]]}
Stacking up layers of (lazy) sequences can run fairly slowly, but the transducers available in Clojure 1.7 will allow you to write faster code in this idiom, as seen in this excellent answer.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
list的dict怎么样?: { 'abc': [2, 6], 'nmb': [3], 'sds': [5] } 在进一步了解OP的用例后进行编辑:您也可以这样做: { 'abc': {2: 3, 6: 7}, 'nmb': {3: 1}, 'sds': {5: 1}, } 您还可以使用defaultdict和collections.Counter如其他答案中所提到的那样,可以快速完成一些工作。 How about a dict of lists?: { ...
-
您可以使用正则表达式: /.*\ (.*)/g | 在这里测试过 完整的工作循环: var str = "key1 val1\nkey2 val2\nkey3 val3"; regex = /.*\ (.*)/g; values = []; while ((result = regex.exec(str))) { values.push(result[1]); } 此外,我个人使用这样的工具进行正则表达式测试,当你开始时它非常方便。 You could use a regex like: /.*\ ...
-
您可以通过将一系列序列转换线程化来完成此操作。 (->> data (group-by #(->> % key (take 2))) vals (map (comp first first (partial sort-by (comp - val)))) (map (juxt #(subvec % 0 2) #(% 2))) (into {})) ;{[0 1] "a", [1 1] "a"} ......在哪里 (def data {[0 1 "a" ...
-
我相信有几个解决方案。 我的第一个想法是使用map-indexed来获取[index character]的列表,然后将集合reduce到地图中。 (defn char-index-map [sz] (reduce (fn [accum [i ch]] (update accum ch conj i)) {} (map-indexed vector sz))) (char-index-map "aabcab") ;;=> {\a (4 1 0), \b (5 2), \ ...
-
为您的场景“制作”的数据结构是B树或其变体,如B +树 。 The data structure that is "made" for your scenario is B-tree or its variants, like B+ tree.
-
下面的函数应该处理这两种情况(假设Vector被作为参数传递给映射): (defn to-map [v] (apply hash-map (if (odd? (count v)) (conj v nil) v))) The below function should take care of both cases (assuming a Vector is passed as an argument to to-map): (defn to ...
-
Clojure排序映射超过值(Clojure sort map over value)[2022-10-25]
在我的Clojure 1.6.0 REPL中,问题中的代码已按值排序: user=> (into (sorted-map-by (fn [key1 key2] (compare [(get x key2) key2] [(get x key1) key1]))) x) {:bla 4, :blub 2, :Foo 2, :Bla 2, :bla/bla 1, :hello 1, :foo 1, :b ... -
这个怎么样: (reduce concat {:a 1 :b [2,3]}) (:a 1 :b [2 3]) 根据amalloy的评论进行更新。 应用更有效(至少在1.4中),并获得相同的结果! (apply concat {:a 1 :b [2,3]}) (:a 1 :b [2 3]) How about this: (reduce concat {:a 1 :b [2,3]}) (:a 1 :b [2 3]) Update based on the comment from amalloy. Ap ...