S S
S S

Reputation: 1513

Property 'filter' does not exist on type '{}

I have an object like this after query. I need to filter it based on the year.

This is my object.

(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", …}

I used following code to filter.

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

But I get am getting error TS2339: Property 'filter' does not exist on type '{}'.

Can anybody help me how to fix this?

Upvotes: 0

Views: 93

Answers (3)

Ganesh
Ganesh

Reputation: 6016

If your already assigning above object to Users.

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

then remove this line

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

your assigning object when it has been already an Array variable

Upvotes: 1

Abdul Basit
Abdul Basit

Reputation: 1382

You cannot apply filter on object

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

This will work

Upvotes: 0

Shiju
Shiju

Reputation: 436

users is of type object. filter method support only in the array object

Also, where is this.user is initialized?

Upvotes: 0

Related Questions