Attach delimited lines only if the lines are not null or empty

It seems like it should be simple, so sorry if I missed something here, but I'm trying to find an easy way to concatenate only non-zero or non-empty strings.

I have several different address fields:

var address; var city; var state; var zip; 

Values ​​for them are set based on some form fields on the page and some other js codes.

I want to output the full address in a div , separated by a comma + space, so something like this:

 $("#addressDiv").append(address + ", " + city + ", " + state + ", " + zip); 

The problem is that one or all of these fields can be empty / empty.

Is there an easy way to combine all non-empty fields in this field group without checking the length of each separately before adding it to the string?

+64
javascript jquery string
Nov 11 '13 at 9:27
source share
7 answers

To consider

 var address = "foo"; var city; var state = "bar"; var zip; text = [address, city, state, zip].filter(Boolean).join(", "); console.log(text) 

stuff.filter(Boolean) (same as .filter(x => x) ) discards zeros, undefined, empty strings, and zeros. If you define "empty", then you will have to set it hard, for example:

  [...].filter(x => typeof x === 'string' && x.length > 0) 

-

(deprecated jquery answer)

 var address = "foo"; var city; var state = "bar"; var zip; text = $.grep([address, city, state, zip], Boolean).join(", "); // foo, bar 
+99
Nov 11 '13 at 9:38
source share

Another one-line solution that doesn't require jQuery :

 var address = "foo"; var city; var state = "bar"; var zip; text = [address, city, state, zip].filter(function (val) {return val;}).join(', '); 
+109
Nov 11 '13 at 10:02
source share

Lodash solution: _.filter([address, city, state, zip]).join()

+11
Jan 16 2018-01-17
source share

Simply:

 [address, city, state, zip].filter(Boolean).join(', '); 
+6
Mar 21 '18 at 15:29
source share

@aga's solution is great, but it doesn’t work in older browsers like IE8 due to the lack of Array.prototype.filter () in its JavaScript engines.

For those who are interested in , an effective solution that works in a wide range of browsers (including IE 5.5 - 8) and does not require jQuery is given below:

 var join = function (separator /*, strings */) { // Do not use: // var args = Array.prototype.slice.call(arguments, 1); // since it prevents optimizations in JavaScript engines (V8 for example). // (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) // So we construct a new array by iterating through the arguments object var argsLength = arguments.length, strings = []; // Iterate through the arguments object skipping separator arg for (var i = 1, j = 0; i < argsLength; ++i) { var arg = arguments[i]; // Filter undefineds, nulls, empty strings, 0s if (arg) { strings[j++] = arg; } } return strings.join(separator); }; 

It includes some performance optimizations described in MDN here .

And here is a usage example:

 var fullAddress = join(', ', address, city, state, zip); 
+5
May 19 '15 at 19:30
source share

Try

 function joinIfPresent(){ return $.map(arguments, function(val){ return val && val.length > 0 ? val : undefined; }).join(', ') } $("#addressDiv").append(joinIfPresent(address, city, state, zip)); 

Demo: Fiddle

+1
Nov 11 '13 at 9:32
source share
 $.each([address,city,state,zip], function(i,v) { if(v){ var s = (i>0 ? ", ":"") + v; $("#addressDiv").append(s); } } );` 
0
Nov 11 '13 at 9:39
source share



All Articles