Angular6 财产';过滤器';不存在于类型';{}

Angular6 财产';过滤器';不存在于类型';{},angular6,Angular6,在查询之后,我有一个这样的对象。我需要根据年份对其进行筛选 这是我的目标 (6) [{…}, {…}, {…}, {…}, {…}, {…}] 0: {title: "PERSONAL", subtitle: "The standard version", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit", year: "2018", …} 1: {title: "STUDENT", subti

在查询之后,我有一个这样的对象。我需要根据年份对其进行筛选

这是我的目标

(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');
}
但是我得到的是
错误TS2339:类型“{}”上不存在属性“filter”。

有人能帮我解决这个问题吗?

用户属于object类型。 仅在数组对象中支持筛选方法


另外,这个用户在哪里初始化?

您不能对对象应用
过滤器

users = [ {year: '2000'}, {year: '2018'} ];
ngOnInit() {
    this.users.filter((user) => user.year == '2018');
}

如果您已经将上述对象分配给用户,这将起作用

users: any;
users =[{…}, {…}, {…}, {…}, {…}, {…}];
然后拆下这条线

// users = {}; remove this line it will work
ngOnInit() {
  this.users.filter((user) => user.year == '2018');
}
当对象已经是数组变量时指定对象