Jump to content

Creating a Name variable consisting of multiple fields


Recommended Posts

I'm still new to js but I'm starting to figure it out.

I know that I could accomplish what I need by creating multiple rules but I'm trying to figure out how to do this within one rule.

 

I often have to create an address block and have multiple fields for the name.

i.e. Prefix, First, Middle, Last, Suffix.

I would like to combine all of those into one field but some of them are empty a fair amount. Can someone tell me how to accomplish this? I'm pretty sure I could do it by creating some super complex else-if statement but I feel like there's probably an easier way.

Link to comment
Share on other sites

  • 2 weeks later...

If you need to call a function from more than one rule, then you should put it into the JavaScript Globals. If it's only used in one rule, then you can either leave it in the rule or put it in the Globals, whichever you prefer.

 

However, in this case, I think a simpler solution would be just:

return ["Prefix", "First", "Middle", "Last", "Suffix"].map(Field).filter(String).join(' ');

This basically says: for this list of strings, call the Field function on each, filter out any empty ones, and delimit them with spaces.

Link to comment
Share on other sites

Thanks Dan.

Yeah I think that's much simpler. But for the Suffix, if I need to add a Comma before, is there a way to do that?

Sure, something like this:

var result = ["Prefix", "First", "Middle", "Last"].map(Field).filter(String).join(' ');
return [result, Field("Suffix")].map(String).join(', ');

Or:

var result = ["Prefix", "First", "Middle", "Last"].map(Field).filter(String).join(' ');
if (!Field("Suffix"))
   return result;

return result + ', ' + Field("Suffix");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...