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
To remove all null values from an array:Declare a results variable and set it to an empty array. Use the forEach() method to iterate over the array. Check if each element is not equal to null . If the condition is satisfied, push the element into the results array.
To remove all undefined values from an array:Use the Array. filter() method to iterate over the array. Check if each value is not equal to undefined and return the result. The filter() method will return a new array that doesn't contain any undefined values.
You can use filter
to filter out the null values:
addresses.filter(function(val) { return val !== null; }).join(", ")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With