I'm currently working on filtering an object that includes a non-mandatory attribute. How can I handle ignoring the attribute if it doesn't exist?
events:IEvent[];
filteredEvents:IEvent[];
performFilter(filterBy:string){
filterBy = filterBy.toLocaleLowerCase();
this.filteredEvents= this.events.filter( e =>
e.location && e.location.city.toLocaleLowerCase().includes(filterBy))
}
I encountered an error when using the code above
ERROR TypeError: e.location is undefined
Event Class
export interface IEvent{
id: number,
name: string,
date: string,
time: string,
price: number,
imageUrl: string,
location?: {
address: string,
city: string,
country: string
},
onlineUrl?: string,
sessions?: ISession[]
}
export interface ISession{
id: number,
name: string,
presenter: string,
duration: number,
level: string,
abstract: string,
voters: string[]
}