Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2:在嵌套数组中按值筛选?_Javascript_Arrays_Angular_Filter_Pipe - Fatal编程技术网

Javascript 角度2:在嵌套数组中按值筛选?

Javascript 角度2:在嵌套数组中按值筛选?,javascript,arrays,angular,filter,pipe,Javascript,Arrays,Angular,Filter,Pipe,我一直在通过Ray Villalobos在Lynda.com()上的一门课程来试验这个GitHub repo——它的功能类似于我希望构建的一个基本web应用程序,但我最近遇到了一点障碍 在上面链接的回购协议中,app/component.app.ts中包含以下数组: var ARTISTS: Artist[] = [ { "name": "Barot Bellingham", "shortname": "Barot_Bellingham", "reknown": "R

我一直在通过Ray Villalobos在Lynda.com()上的一门课程来试验这个GitHub repo——它的功能类似于我希望构建的一个基本web应用程序,但我最近遇到了一点障碍

在上面链接的回购协议中,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=''">
因此,我希望按“Harry”进行过滤,并且只显示在“姓名”、“重名”、“朋友”、“电子邮件”或“汽车”中包含“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方法中的第一行代码确保传入的修饰符也是小写的,以便compare始终比较小写值。它还有一个空检查,以确保它在为空时不会尝试将其小写

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

我将
includes
更改为
indexOf
<代码>包括检查数组。这些项(例如eachItem['name'])是数组吗

这应该更接近


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

非常感谢您的持续帮助和解释,DeborahK!我将在一天左右的时间内做出一个扑救。然而,我使用了您修改过的代码,但它阻止了应用程序加载,我也不明白为什么(控制台显示意外分号)。看见当我使用问题中提供的过滤器时,不会触发此操作。再次感谢!是否显示其他错误?这是一个副作用,而不是实际的错误。不,这是控制台中显示的唯一错误,它阻止了应用程序加载。只有“加载…”在显示,这是我的
标签中的内容。然后一个plunker将是最有用的。太好了;我希望明天能拿到。我实际上只是在使用来自的文件。也许我需要重新开始。
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...
]
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;
    });
  }
}