用ngModel过滤JSON

用ngModel过滤JSON,json,angular,filter,Json,Angular,Filter,我目前正在过滤我的JSON,如果我用一个硬编码的值过滤它,它工作得很好 代码示例: this.names= this.names.filter(function (name) { return name.last== 'lastname'; }); 它返回最后一个键的值为“lastname”的对象。但是,当我使用变量进行筛选时 代码: 它在控制台错误中显示无法读取“selectedLname”的属性,但selectedLname有一个值这是因为中的该关键字引用了该函数对象。尝试将您

我目前正在过滤我的JSON,如果我用一个硬编码的值过滤它,它工作得很好

代码示例:

this.names= this.names.filter(function (name) {
    return name.last== 'lastname';
  });
它返回最后一个键的值为“lastname”的对象。但是,当我使用变量进行筛选时

代码:


它在控制台错误中显示无法读取“selectedLname”的属性,但selectedLname有一个值

这是因为
中的该
关键字引用了该函数对象。尝试将您的函数更改为箭头函数,它将起作用

this.names = this.names.filter((name) => {
    return name.last== this.selectedLname';
});
var数据=[{
名字:'abc',
姓氏:“xyz”
},
{
名字:'yut',
姓氏:“eueo”
}];
var seachKey='xyz'
var result=data.filter((item)=>item.lastName==seachKey);

控制台日志(结果)现在它显示了这个.names.filter不是控制台中的一个函数。您可以显示如何调用此函数吗?当(单击)FooterUpper解决方案中的按钮时,我认为如果您出现上述错误,唯一的原因是当您的names数组为null时。请检查一下
this.names = this.names.filter((name) => {
    return name.last== this.selectedLname';
});