I am having a javascript array.
addresses = new Array(document.client.cli_Build.value,
document.client.cli_Address.value,
document.client.cli_City.value,
document.client.cli_State.value,
document.client.cli_Postcode.value,
document.client.cli_Country.value);
document.client.cli_PostalAddress.value = addresses.join(", ");
I have to copy the content of all these array value to the postal address textarea. when i use the above join function, comma has been added for null values. How to remove this extra commas?
Thanks
Another filter alternative
If you would like to eliminate all the undefined, null, NaN, "", 0, a simple way to do it is to use a combination of filter call back function and boolean function.
When you pass a value to the boolean function, if the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.
Here is an example of this usage:
Here are some tests:
addresses.filter(Boolean).join(", ")
Use the following code to remove the
null
values only, its short & simple:If you want to remove
null
,0
,false
&""
(Empty String) like values, then use this:Use
filter
method to remove all falsy values:You can use
filter
to filter out the null values: