2

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!

Dave
  • 1,257
  • 2
  • 27
  • 58
  • not sure what the down vote without a comment is for, but would still love to know if this is possible – Dave May 24 '18 at 15:52
  • Does your current object only contain those properties? – NocNit May 24 '18 at 15:55
  • How do you know whether your user has input something into them? – Bergi May 24 '18 at 15:58
  • [related](https://stackoverflow.com/a/50427492/1048572) – Bergi May 24 '18 at 16:01
  • The implicit not-quite-normal-javascript thing going on here is that `current` is a `GlideRecord` instance. So, dot-walks into fields (eg agency_street) are going to behave a little differently since they are returning GlideElement instances. They will never be null, but concatenating to a string is going to return an empty string if the internal field value is null (otherwise it'll return the toString'd value like you'd expect). – Joey May 24 '18 at 16:05

4 Answers4

5

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/

dmgig
  • 4,400
  • 5
  • 36
  • 47
  • 1
    Thanks!! the arrow syntax doesn't work for us, but I used return values.filter(Boolean).join(', '); and it works. thanks again for this! – Dave May 24 '18 at 16:18
  • Great! Very good - and I learned something as well about `filter(Boolean)`. So thank you. – dmgig May 24 '18 at 16:36
2

As always a loop helps to simplify things:

let result = "";
for(const key of ["street", "city", "state", "zip"]) {
  const value = current["agency_" + key];
  if(value) result += (result && ",") + value;
}
return result;

Which is maybe a bit more elegant with functional style:

return ["street", "city", "state", "zip"].map(key => current["agency_" + key]).filter(Boolean).join(", ");
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

You can use this if only the filled in properties of the object are set and the order of the results doesn't strictly matter:

Object.keys(current).map(key => current[key]).filter(val => val).join(", ");
NocNit
  • 280
  • 1
  • 7
0

You can use ternary operator as well

return (current.agency_street ? current.agency_street : '') + (current.agency_city ? ',' + current.agency_city : '') + (current.agency_state ? ',' + current.agency_state : '') + (current.agency_zip ? ',' + current.agency_zip : '');
Jay
  • 338
  • 1
  • 5