Created
December 31, 2011 06:11
-
-
Save nuwansh/1543092 to your computer and use it in GitHub Desktop.
Remove empty elements from an array in Javascript and join it as string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**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" |
Author
nuwansh
commented
Dec 31, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment