【问题标题】:Angular 2: Filter by value in nested array?Angular 2:按嵌套数组中的值过滤?
【发布时间】:2017-11-26 06:50:53
【问题描述】:

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

在上面链接的那个 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”进行过滤,仅显示“姓名”、“知名”、“朋友”、“电子邮件”或“汽车”中包含“哈利”的对象。这可能吗?如果可以,我该如何编辑管道过滤器来做到这一点?谢谢!!

(总的来说,我在 Angular 和 JS 方面很菜,所以如果我使用了不正确的术语或忽略/误解了一些基本的东西,我想提前道歉。)

【问题讨论】:

    标签: javascript arrays angular filter pipe


    【解决方案1】:

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

    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 时尝试将其小写。

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

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

    那应该更近了。

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

    【讨论】:

    • 非常感谢您一直以来的帮助和解释,DeborahK!我会在一天左右的时间内做一个 plunker。但是,我使用了您修改后的代码,但它阻止了应用程序的加载,我不知道为什么(控制台显示意外的分号)。见screenshot of console error (imgur.com。当我使用我的问题中提出的过滤器时,这不会触发。再次感谢!
    • 是否显示其他错误?这是一个副作用,而不是实际错误。
    • 不,这是控制台中显示的唯一错误,它会阻止应用程序加载。仅显示“正在加载...”,这是我的 &lt;app&gt;&lt;/app&gt; 标签内的内容。
    • 那么一个plunker将是最有帮助的。
    • 太棒了;我希望明天能做到。我实际上只是使用来自this GitHub repository 的文件。也许我需要重新开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多