Writing Expressions : Manipulating string data : Removing null values from combined fields
 
Removing null values from combined fields
When concatenating string values, JavaScript converts null values to the word null. The example report in Figure 11‑7 displayed addresses with the word null when the addressLine2 field did not contain a value, for example:
8490 Strong St., null, Las Vegas, NV 83030, USA
Remove the word null by using the replace( ) function. In this example, use replace( ) in the expression to search for “null, ” and replace it with an empty string. You should also search for the comma and space after null to remove the extra comma and space that is added after the addressLine2 field. If you search only for “null” you get the following results:
8490 Strong St., , Las Vegas, NV 83030, USA
Use the following expression to remove null values from a concatenated address:
(row["addressLine1"] + ", " + row["addressLine2"] + ", " + row["city"] + ", " + row["state"] + " " + row["zip"] + ", " + row["country"]).replace("null, ","")
Searching for and replacing “null, ” does not, however, take into account missing values in the state and country fields. The state value does not have a comma after it, so you need to search for “null ”. The country value does not have a comma or space after it, so you need to search for “null”.
To replace null values in the state and country fields, add two more replace( ) functions to the expression, as follows:
(row["addressLine1"] + ", " + row["addressLine2"] + ", " + row["city"] + ", " + row["state"] + " " + row["zip"] + ", " + row["country"]).replace("null, ","").replace("null ","").replace("null","")