Jump to content

Suppress Empty variables


LyndaEngelage

Recommended Posts

I'm an extreme novice at scripting. But I need a rule to suppress the line if ALL variables are empty. The following is a script I'm trying out. I need the following format to happen: "Mem_Lastname", "Mem_Firstname" "Mem_MiddleInit"

 

Example: Doe, Joe C

 

(I need the comma inserted between the Mem_Lastname and Mem_First name fields.)

I need the whole line suppressed if all the fields are empty, not just one, including the comma.

 

Here's the script I have so far.

 

 

return [Trim(Field("Mem_Lastname")),Trim(Field("Mem_Firstname")),Trim(Field("Mem_MiddleInit"))].filter(String).join(" ");

Link to comment
Share on other sites

Here's the script I have so far.

return [Trim(Field("Mem_Lastname")),Trim(Field("Mem_Firstname")),Trim(Field("Mem_MiddleInit"))].filter(String).join(" ");

 

One nice thing about the filter method is that you can pass it a callback function to determine whether to remove the element from the array or not. For example, if you wanted to include the comma as part of the first element in your array (the lastname field), you could write a function to filter the elements if they are not strings or if the string is only a comma indicating that the last name field is empty:

return [
 Trim(Field("Mem_Lastname"))[color="Red"] + ','[/color],
 Trim(Field("Mem_Firstname")),
 Trim(Field("Mem_MiddleInit"))
].filter([color="red"]function(s){ 
   return s.replace(',', '');
 }[/color]).join(' ');

 

Using the .map method would allow you to perform a function on each element of the array. This is helpful in situations that you might find yourself being repetitive in your code – with the Trim function for example. You could also add the comma to the first element of your array within that function like this:

return [
 Field("Mem_Lastname"),
 Field("Mem_Firstname"),
 Field("Mem_MiddleInit")
][color="red"].map(function(s,p) {
 return Trim(s) ? Trim(s) + (!p ? ',' : '') : '';
})[/color].filter(String).join(' ');

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...