Skip to content
Advertisement

Function parameter is not defined in callback

I am trying to get the location of the user and using that I am trying to get the city. I don’t why but when I call the query function, the city parameter is having some value but it is not reflected in the callback function’s if condition. However, if I replace the city variable in the If condition with the common String callback function works fine. the data variable is the array of objects

JavaScript

Advertisement

Answer

You have to return false in the filter, if it is not passing your condition – thus, it is better to just write:

JavaScript

In this case the return value is going to be Boolean (true if passes, false if not).

Also, the data variable is not defined in the function. It’s better to pass it in as an argument.

And also: the objects in the data array have a key called Specialities, but you are filtering for Specialties

JavaScript

EDIT: AVOIDING TYPOS

There’s a way to avoid typos like the one in your code: use constants:

JavaScript

This way you can choose easier words instead of complicated strings.

EDIT 2

Also, you could update this solution if you’d set up the function(s) a bit differently:

JavaScript

This solution seems a bit better if you filter the data source for a certain type of hospital many times over. As you create functions by passing in arguments one by one they are cached (by V8), so working with them becomes faster. (At least theoretically.)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement