Our team wants to generate a concatenated address based on a user's input for street, city, state, and zip. The issue is many times a user may not fill out all four of these inputs. Is there a way to efficiently generate all combinations for the concatenated address field without writing 12 if statements?
For example, if a user has all 4 fields filled out, we want to return this:
return current.agency_street + ', ' + current.agency_city + ', ' + current.agency_state + ', ' + current.agency_zip;
If they have everything filled out except zip code, we want to return this:
return current.agency_street + ', ' + current.agency_city + ', ' + current.agency_state;
Is there a way to do this without all the if statements? Thanks!
You can push all to an array and filter out the falsy values.
var values = [];
values.push(agency_street);
values.push(agency_city);
values.push(agency_state);
values.push(agency_zip);
return values.filter(x => x).join(', ');
fiddle: https://jsfiddle.net/xfqumxox/