【问题标题】:Filtering JSON by ngModel通过 ngModel 过滤 JSON
【发布时间】:2021-03-12 15:10:46
【问题描述】:

我目前正在过滤我的 JSON,如果我使用硬编码值过滤它就可以正常工作。

代码示例:

this.names= this.names.filter(function (name) {
    return name.last== 'lastname';
  });

它返回最后一个键的值为'lastname'的对象。但是,当我使用变量进行过滤时

代码:

this.names= this.names.filter(function (name) {
    return name.last== this.selectedLname';
  });

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

【问题讨论】:

    标签: json angular filter


    【解决方案1】:

    这是因为 this 关键字引用该函数对象。尝试将您的功能更改为箭头功能,它会工作。

    this.names = this.names.filter((name) => {
        return name.last== this.selectedLname';
    });
    

    var data= [ {
      firstName:'abc',
      lastName: 'xyz'
    },
    {
      firstName:'yut',
      lastName: 'eueo'
    }];
    var seachKey = 'xyz'
    var result = data.filter((item) => item.lastName == seachKey);
    console.log(result);

    看看这个

    【讨论】:

    • 现在显示 this.names.filter 不是控制台中的函数
    • 你能说明你是如何调用这个函数的吗?
    • 在页脚中的按钮上(单击)
    • 上述解决方案应该有效,如果您遇到上述错误,我能想到的唯一原因是您的名称数组为空。请检查
    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多