I am kind of confusing about the jquery.map function.
I have an array say myArray = ["1", 3, "a", 5].
Can I use $.map().get().join(',') to get result of [1, 3, 5];
$(myArray).map(function() {
if (!isNaN(this)){
return this
}
}).get().join(',');
the above code return ["1","3","a","5"]
http://jsfiddle.net/kkgian/khurZ/
TIA
.map()does not alter the original array. Because your code does not capture the value returned, you’re just throwing it away.Use the (poorly-named)
$.grepfunction instead of.map(). Thanks to theinvertargument, you don’t even have to use an anonymous function; you can useisNaNdirectly as the predicate.http://jsfiddle.net/mattball/nzeBZ/
N.B.
isNaN('1')returnsfalsebecause'1'is a string that can be reasonably coerced to a number. If that’s not acceptable, use a better filter predicate. It’s a little unclear if you actually want an array of numbers, or just the string'[1, 3, 5]', however.