I know you can do this through looping through elements of array and concatenating. But I\'m looking for one-liner solutions. toString() and join() returns string with eleme
The easiest way is to use .join(' ').
However, if the Array contains zero-length objects like null, the following code would avoid multiple spaces:
arr.filter(i => [i].join(" ").length > 0).join(" ");
Here's some example usage:
Array.prototype.merge = function(char = " ") {
return this.filter(i => [i].join(" ").length > 0).join(char);
};
console.log(["a", null, null, "b"].merge());