Skip to content

Instantly share code, notes, and snippets.

@hanigamal
Forked from nuwansh/remove_empty_value.js
Last active August 29, 2015 14:27
Show Gist options
  • Save hanigamal/d48dd548ea54c302ed2a to your computer and use it in GitHub Desktop.
Save hanigamal/d48dd548ea54c302ed2a to your computer and use it in GitHub Desktop.
Remove empty elements from an array in Javascript and join it as string
/**This is example works with only jQuery
* @usecase, if you have an array with empty values (ex: ["I", "", "want", "", "to", "go", "" ])
* but you want to return this array as a string.
*/
var myRoom = {
myAction: function(array){
garray = $.grep(array,function(n){
return(n);
});
//join by ' '
return garray.join(' ');
}
};
var string = myRoom.myAction(["I", "", "want", "", "to", "go", "" ]);
console.log(string);
//Out put is "I want to go"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment