Accessibility First DevRel. I focus on ensuring content created, events held and company assets are as accessible as possible, for as many people as possible.
This example needs a little bit of a rethink as at the moment you are comparing apples and oranges.
The first function searches one field whereas the second searches all fields.
Secondly the second function returns partial matches which may not be desirable.
Lets say I wanted to search for only properties in the state "rivers", in the first example I would do findOrdersByState ("rivers"); and get the expected result.
In the second example I might have a town called "tranquil rivers" and it would also be returned.
Finally the first code would be far more performant as you aren't having to loop through each object parameter.
Perhaps the following would be a better example (untested)?
functionfindOrderByAddressField(value,field){field=field||"state";letfoundOrders=[]orders.forEach(order=>{if(order.address[field]==value){foundOrders.push(order)}})returnfoundOrders}constriversOrders=findOrderByAddressField('rivers','state')// find order by stateconstphOrders=findOrderByAddressField('port harcourt','city')// find orders by cityconstNigerianOrders=findOrderByAddressField('nigeria','country')// find orders by countryconstriversOrders2=findOrderByAddressField('rivers')// find orders by state by default
Obviously I had to add a check for a null "field" otherwise it could throw an error, the advantage is it now has a default if you want (if you didn't want this behaviour you could just return false if it wasn't set).
Obviously it would still throw an error if an invalid field was entered, so at that point you might also want to check that depending on what behaviour you want. (i.e. findOrderByAddressField('rivers', 'area') would throw an error);
I see, this wouldn't be the final solution, and if indexOf returns partial matches, you can use array.find() instead.
functionfindOrderByAddressField(field){letfoundOrders=[]letfoundOrder;orders.forEach(order=>{foundOrder=Object.values(order.address).find(v=>v===field)foundOrders.push(foundOrder)})returnfoundOrders}constriversOrders=findOrderByAddressField('rivers')// find order by stateconstphOrders=findOrderByAddressField('port harcourt')// find orders by cityconstNigerianOrders=findOrderByAddressField('nigeria')// find orders by country
Accessibility First DevRel. I focus on ensuring content created, events held and company assets are as accessible as possible, for as many people as possible.
You still have the issue of matching across all fields in the above example, so a search for "rivers" would match on state, city and country.
Collisions would be rare but if this was something else like names with first name and surname fields, you could search for "Frank" and it would match "Frank Johnson" and "John Frank".
The code I wrote was to mirror your original functions where each one was for a specific field but your function is ideal for if you want to search across all 3 fields at once, it just doesn't do the same as the first functions so isn't ideal as a comparison.
Obviously it all depends on what the end use case is!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This example needs a little bit of a rethink as at the moment you are comparing apples and oranges.
The first function searches one field whereas the second searches all fields.
Secondly the second function returns partial matches which may not be desirable.
Lets say I wanted to search for only properties in the state "rivers", in the first example I would do
findOrdersByState ("rivers");
and get the expected result.In the second example I might have a town called "tranquil rivers" and it would also be returned.
Finally the first code would be far more performant as you aren't having to loop through each object parameter.
Perhaps the following would be a better example (untested)?
Obviously I had to add a check for a null "field" otherwise it could throw an error, the advantage is it now has a default if you want (if you didn't want this behaviour you could just return false if it wasn't set).
Obviously it would still throw an error if an invalid field was entered, so at that point you might also want to check that depending on what behaviour you want. (i.e.
findOrderByAddressField('rivers', 'area')
would throw an error);I see, this wouldn't be the final solution, and if
indexOf
returns partial matches, you can usearray.find()
instead.You still have the issue of matching across all fields in the above example, so a search for "rivers" would match on state, city and country.
Collisions would be rare but if this was something else like names with first name and surname fields, you could search for "Frank" and it would match "Frank Johnson" and "John Frank".
The code I wrote was to mirror your original functions where each one was for a specific field but your function is ideal for if you want to search across all 3 fields at once, it just doesn't do the same as the first functions so isn't ideal as a comparison.
Obviously it all depends on what the end use case is!