简体   繁体   English

多维json对象的Javascript过滤器

[英]Javascript filter for multidimensional json object

Can't use javascript filter in multi-dimensional object.不能在多维对象中使用 javascript 过滤器。

 var object = [{ "id": "1", "name": "General", "cards": [{ "id": "1", "name": "shawn" }, { "id": "2", "name": "neo" }] }, { "id": "2", "name": "CEO", "cards": [{ "id": "1", "name": "Raman" }, { "id": "2", "name": "Sheena" }] }] function searchFor(item) { return item.cards.filter( (card) => { return card.name.indexOf("Raman") !== -1; } ); } var filtered = object.filter(searchFor); console.log(filtered);

This is how I am trying, inside the searchFor card.name I am getting the correct card name but filtering is returning all the cards.Its not filtering.这就是我正在尝试的方式,在 searchFor card.name 中,我得到了正确的卡片名称,但过滤正在返回所有卡片。它不是过滤。

Could any help me with this.任何人都可以帮我解决这个问题。

An empty array isn't considered falsey in Javascript.空数组在 Javascript 中不被认为是错误的。 So instead of returning the result of filtering the cards array, test its length.因此,与其返回过滤cards数组的结果,不如测试其长度。

 var object = [{ "id": "1", "name": "General", "cards": [{ "id": "1", "name": "shawn" }, { "id": "2", "name": "neo" }] }, { "id": "2", "name": "CEO", "cards": [{ "id": "1", "name": "Raman" }, { "id": "2", "name": "Sheena" }] }] function searchFor(item) { return item.cards.filter( (card) => { return card.name.indexOf("Raman") !== -1; } ).length != 0; } var filtered = object.filter(searchFor); console.log(filtered);

You were returning the filtered array, which would produce a TRUE result whenever cards existed.您正在返回过滤后的数组,只要cards存在,就会产生TRUE结果。 So you can just turn that into a boolean, by saying when the item.cards.filter(...).length > 0 .所以你可以把它变成一个布尔值,通过说当item.cards.filter(...).length > 0

 var object = [{ "id": "1", "name": "General", "cards": [{ "id": "1", "name": "shawn" }, { "id": "2", "name": "neo" }] }, { "id": "2", "name": "CEO", "cards": [{ "id": "1", "name": "Raman" }, { "id": "2", "name": "Sheena" }] }] var searchFor = (card) => card.name.indexOf("Raman") > -1; var filteredCards = object.reduce((cards, item) => cards.concat(item.cards.filter(searchFor)), []); var filteredObj = object.map(i => { i.cards = i.cards.filter(searchFor); return i; }).filter(i => i.cards.length) console.log(filteredCards, filteredObj)

Updated更新

I updated the code snippet to produce either the cards which were found.我更新了代码片段以生成找到的卡片。 I also provide a method for returning all objects which contain the needed cards, and filter out the other cards.我还提供了一种方法来返回包含所需卡片的所有对象,并过滤掉其他卡片。

// HTML Part // HTML 部分

  <div class="filter-list">
    <button class="filter" data-filter-key="all">all</button>
    <button class="filter" data-filter-key="open">open</button>
    <button class="filter" data-filter-key="done">done</button>
  </div>

// CSS Part // CSS 部分

.filter:hover,
.filter:focus,
[data-active-filter="all"] .filter[data-filter-key="all"],
[data-active-filter="done"] .filter[data-filter-key="done"],
[data-active-filter="open"] .filter[data-filter-key="open"] {
  text-decoration: underline;
}

[data-active-filter="open"] [data-completed="true"],
[data-active-filter="done"] [data-completed="false"] {
  display: none;
}

// Script Part // 脚本部分

(function () {
  const mainNode = document.querySelector("main");
  const filters = document.querySelector(".filter-list");

  for (const filter of filters.children) {
    filter.addEventListener("click", () => {
      mainNode.setAttribute(
        "data-active-filter",
        filter.getAttribute("data-filter-key")
      );
    });
  }

  mainNode.setAttribute("data-active-filter", "all");
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM