首页 > 解决方案 > 类型“{}”上不存在属性“过滤器”

问题描述

查询后我有一个这样的对象。我需要根据年份过滤它。

这是我的对象。

(6) [{…}, {…}, {…}, {…}, {…}, {…}]
 0: {title: "PERSONAL", subtitle: "The standard version", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit", year: "2018", …}
    1: {title: "STUDENT", subtitle: "Most popular choice", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit", year: "2017", …}
    2: {title: "BUSINESS", subtitle: "For the whole team", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit", year: "2018", …}

我使用以下代码进行过滤。

users = {};
ngOnInit() {
    this.users.filter((user) => user.year == '2018');
}

但我得到了error TS2339: Property 'filter' does not exist on type '{}'.

谁能帮我解决这个问题?

标签: angular6

解决方案


如果您已经将上述对象分配给用户。

users: any;
users =[{…}, {…}, {…}, {…}, {…}, {…}];

然后删除此行

// users = {}; remove this line it will work
ngOnInit() {
  this.users.filter((user) => user.year == '2018');
}

当它已经是一个数组变量时你的分配对象


推荐阅读