I am dealing with JSON data structured as follows:
jsonList= [
{name:'chennai', code:'maa'}
{name:'delhi', code:'del'}
....
....
....
{name:'salem', code:'che'}
{name:'bengaluru',code:'blr'}
}]
My goal is to filter this data based on keys (name, code) and return matching values. For instance, if CHE is provided, I should first check the CODE for matches and then move on to the NAME.
{name:'salem', id:'che'},
{name:'chennai', id:'maa'}
While attempting to achieve this, I encountered issues with my code. It seems to only check on the NAME each time it runs.
public filterJson(text){
this.filteredOptions = this.jsonList.filter(
e => (
e.code.toLowerCase().indexOf(text.toString().toLowerCase()) !== -1) ||
e.name.toLowerCase().indexOf(text.toString().toLowerCase()) !== -1)
).slice(0, 9);
}
I also tried using 0 as a test case for filtering:
public filterJson(text){
this.filteredOptions = this.jsonList.filter(
e => (
e.code.toLowerCase().indexOf(text.toString().toLowerCase()) === 0) ||
e.name.toLowerCase().indexOf(text.toString().toLowerCase()) === 0)
).slice(0, 9);
}
If you want to see more details or try out the TypeScript playground link, click here