简体   繁体   English

角度2:按嵌套数组中的值过滤?

[英]Angular 2: Filter by value in nested array?

I've been experimenting with this GitHub repo via a course on Lynda.com ( https://github.com/planetoftheweb/learnangular ) by Ray Villalobos -- it functions similarly to a basic web app that I'm hoping to build, but I've recently hit a bit of a road block. 我一直在通过Ray Villalobos的Lynda.com( https://github.com/planetoftheweb/learnangular )上的一门课程来尝试这个GitHub存储库-它的功能类似于我希望构建的基本网络应用程序,但我最近遇到了一些障碍。

In that repo linked above, in app/component.app.ts, is the following array: 在上面链接的该仓库中,在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...
]

This array is filtered by a pipe as seen in app/pipe.search.ts: 如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());
    });
  }
}

Here's the filter input: 这是过滤器输入:

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

And the code for the filter results: 以及过滤结果的代码:

<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>

This all works perfectly, however, in my project, I would need to include three nested arrays, and have the ability to filter based upon the values in those arrays. 所有这些都可以完美地运行,但是,在我的项目中,我需要包括三个嵌套数组,并且能够根据这些数组中的值进行过滤。 A sample of the kind of array I need will look something like this: 我需要的那种数组的样例将如下所示:

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...
]

Therefore, I hope to filter by “Harry,” and only display objects that contain “harry” in either “name,” “reknown,” “friends,” "emails," or "cars." 因此,我希望按“哈利”进行过滤,只显示“名称”,“知名”,“朋友”,“电子邮件”或“汽车”中包含“哈利”的对象。 Is this possible, and if so, how can I edit the pipe filter to do this? 这有可能吗?如果可以,我该如何编辑管道过滤器来做到这一点? Thank you!! 谢谢!!

(I'm pretty green at angular and JS in general, so I want to apologize in advance if I've used incorrect terminology or overlooked/misunderstood something basic.) (我一般在Angular和JS上都是绿色的,因此,如果我使用了错误的术语或对基本知识的忽视/误解,我想提前道歉。)

I deleted my prior answer because it was more confusing than helpful. 我删除了先前的答案,因为它比帮助更令人困惑。 I pasted example code without applying it to your variables/properties/objects and it was misleading. 我粘贴示例代码时未将其应用于变量/属性/对象,这具有误导性。 Let's try again: 让我们再试一次:

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;
    });
  }
}

The first line of code in the transform method ensures that the modifier passed in is also lowercase so that the compare always compares lower case values. 转换方法中的第一行代码确保传入的修饰符也为小写,以便比较始终比较小写值。 It also has a null check to ensure it does not try to lowercase it if it is null. 它还具有null检查,以确保如果它为null则不尝试将其小写。

The second line of code also uses the "?" 第二行代码也使用“?” syntax to handle the case of a null pipeModifier. 用于处理空pipeModifier情况的语法。

I changed includes to indexOf . 我将includes更改为indexOf Includes checks arrays. Includes检查数组。 Are these items, such as eachItem['name'], an array? 这些项目(例如eachItem ['name'])是否为数组?

That should be closer. 那应该更近了。

NOTE: Without a provided plunker ... I did not check the syntax or correct execution of this code. 注意:没有提供的插件...我没有检查语法或该代码的正确执行。

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

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