I have data that is in an array of structs. I don't have access to a query variable, and I need to filter down the data. So var I have
arData = arData.filter(
function(item){
return (form.searchPhrase == ""
||
item.name CONTAINS form.searchPhrase
||
item.company CONTAINS form.searchPhrase
||
item.address CONTAINS form.searchPhrase
||
item.address2 CONTAINS form.searchPhrase
||
item.city CONTAINS form.searchPhrase
||
item.state CONTAINS form.searchPhrase
||
item.zip CONTAINS form.searchPhrase
||
item.email CONTAINS form.searchPhrase
||
item.tel CONTAINS form.searchPhrase
);
});
I am going over all the fields in item.
What I don't like about this is that it seems highly repetitive. It is likely that a new struct key could be added. Or an existing be removed. I am looking for a cleaner way to do this.
Why not just loop over the item's properties then?
In case your item might contain properties with a
null
value (for example if you read JSON from an external API), you want to checkstructKeyExists(item, itemProperty)
in the loop as well.