Jump to content

Suppress line if multiple fields are empty


Recommended Posts

Using Acrobat Pro DC with FusionPro version 10.1.11

 

I need to join the State and zip with a different "join" of just a space.

 

The following code works except it joins STATE and ZIP CODE with ", "

Is there a way to differentiate the joins.

 

var items = [ Field("STREET"), Field("CITY"), Field("STATE"), Field("ZIP CODE") ];

items = items.filter(String); // remove empty items

return [ items.slice(0, 4).join(", "), items.slice(4).join(" ") ].filter(String).join("<br>\n");

Link to comment
Share on other sites

So, I'm usually in favor of clever ways to use arrays and such to abstract things and reduce repetitive code. If this were something that could be possibly extended to handle more fields in the future, that would be a great way to go. But in this case, you're only ever going to have these few fields, so it might be better to be more straightforward.

 

If you only need a single space, just put the space there:

return items.slice(0, 3).join(", ") + " " + items.slice(3);

Though really, even that seems a bit unnecessarily complicated. You can be even more straightforward. The whole thing could be one line:

return [ Field("STREET"), Field("CITY"), Field("STATE") ].join(", ") + " " + Field("ZIP CODE");

Though even that doesn't save much from the most straightforward approach:

return Field("STREET") + ", " + Field("CITY") + ", " + Field("STATE") + " " + Field("ZIP CODE");

I also don't think you need the call to remove empty fields; an address without any of those would presumably be invalid. Unless there's an "Address 2" line, in which case I would just handle that as a special case:

var streetAddr = [ Field("Address1"), Field("Address2") ].filter(String);
return [ streetAddr, Field("CITY"), Field("STATE") ].join(", ") + " " + Field("ZIP CODE");

Edited by Dan Korn
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...