How can I make this work? I have an space in the third element of my array ” John”.
take a look:
http://jsfiddle.net/hyHFT/
<style>
div { color:blue; }
span { color:red; }
</style>
<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<script>var arr = [ 4, " Pete", 8, " John" ];
$("span:eq(0)").text(jQuery.inArray("John", arr));
$("span:eq(1)").text(jQuery.inArray(4, arr));
$("span:eq(2)").text(jQuery.inArray("Karl", arr));
</script>
Try doing this:
mapfunction appliestrimto every element in the array, and stores its result (the string without spaces) into a new array, in the same position.Having your array sanitized, you’ll find those elements.
Just a sidenote, it will convert the number to a string. Take care of that.
Good luck!
Edit:
If you want to keep the original type, use this:
self-explanatory, isn’t it?