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.
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!