javascript - Angular 2 : Filter by value in nested array?

标签 javascript arrays angular filter pipe

我一直在通过 Ray Villalobos 在 Lynda.com (https://github.com/planetoftheweb/learnangular) 上的类(class)试验这个 GitHub 存储库——它的功能类似于我希望构建的基本 Web 应用程序,但我最近遇到了一些障碍。

在上面链接的 repo 中,在 app/component.app.ts 中,是以下数组:

var ARTISTS: Artist[] = [
  {
    "name": "Barot Bellingham",
    "shortname": "Barot_Bellingham",
    "reknown": "Royal Academy of Painting and Sculpture",
    "bio": "Some bio here."
  },
  // etc...
]

如 app/pipe.search.ts 中所示,此数组由管道过滤:

export class SearchPipe implements PipeTransform {
  transform(pipeData, pipeModifier) {
    return pipeData.filter((eachItem) => {
      return eachItem['name'].toLowerCase().includes(pipeModifier.toLowerCase()) ||
        eachItem['reknown'].toLowerCase().includes(pipeModifier.toLowerCase());
    });
  }
}

这是过滤器输入:

<input class="search-input" [(ngModel)]="field1Filter" placeholder="type in search term here" (click)="showArtist(item); field1Filter=''">

以及过滤结果的代码:

<ul class="artistlist cf" *ngIf="field1Filter">
  <li class="artistlist-item cf"
     (click)="showArtist(item);"
     *ngFor="let item of (artists | search: field1Filter)"> 
    <artist-item class="content" [artist]=item></artist-item>
  </li>
</ul>
<artist-details  *ngIf="currentArtist" [artist]="currentArtist"></artist-details>

这一切都很完美,但是,在我的项目中,我需要包含三个嵌套数组,并且能够根据这些数组中的值进行过滤。我需要的数组示例如下所示:

var ARTISTS: Artist[] = [
  {
    "name": "Barot Bellingham",
    "shortname": "Barot_Bellingham",
    "reknown": "Royal Academy of Painting and Sculpture",
    "bio": "Some bio here...",
    "friends": [
      "James",
      "Harry",
      "Bob",
      "Liz",
      "Kate",
      "Jesse"
    ],
    "emails": [
      "bb@this.com",
      "aa@this.com"

    ],
    "car": [
      "honda",
      "scion",
      "aston martin"
    ]

  },
  // etc...
]

因此,我希望按“Harry”过滤,只显示在“name”、“reknown”、“friends”、“emails”或“cars”中包含“harry”的对象。这可能吗?如果可以,我该如何编辑管道过滤器来执行此操作?谢谢!!

(我对 Angular 和 JS 总体上还很陌生,所以如果我使用了不正确的术语或忽略/误解了一些基本的东西,我想提前道歉。)

最佳答案

我删除了我之前的答案,因为它更令人困惑而不是有用。我粘贴了示例代码,但没有将其应用于您的变量/属性/对象,这是一种误导。让我们再试一次:

export class SearchPipe implements PipeTransform {
  transform(pipeData, pipeModifier) {
    pipeModifier = pipeModifier ? pipeModifier.toLowerCase() : null;
    return pipeModifier ? pipeData.filter(eachItem => {
      eachItem['name'].toLowerCase().indexOf(pipeModifier) !== -1 ||
      eachItem['reknown'].toLowerCase().indexOf(pipeModifier !== -1) : pipeData;
    });
  }
}

transform 方法中的第一行代码确保传入的修饰符也是小写的,以便比较始终比较小写值。它还具有 null 检查以确保它不会在它为 null 时尝试将其小写。

第二行代码也使用了“?”处理空 pipeModifier 情况的语法。

我将 includes 更改为 indexOfIncludes 检查数组。这些项目,例如 eachItem['name'],是一个数组吗?

那应该更近一些。

注意:没有提供 plunker ... 我没有检查语法或正确执行此代码。

关于javascript - Angular 2 : Filter by value in nested array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44708754/

相关文章:

javascript 逻辑错误,我的 html .text() 在提交时未替换旧文本

javascript - 无法在某个点停止进度条

c++ - 有什么明确的解释吗?

java - Angular 6 访问 REST 失败,Access-Control-Allow-Origin

javascript - 有人可以解释闭包的 js 变量提升和堆栈执行吗

javascript - 如何在滚动条上应用 css?

C# 循环遍历给定标签的数组

php - PDO:多。行和列 - 如何循环数组?

java - 在 Angular 应用程序中获取 JSESSIONID

node.js - 当属性准确显示在页面上时出现属性不存在的错误