1

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>
1
  • Is removing the spaces before you put the string into the array out of the question? Commented Jun 16, 2011 at 23:14

6 Answers 6

3

Try doing this:

var arr = [ 4, "  Pete", 8, "  John" ];
arr = $(arr).map(String.prototype.trim);
...

map function applies trim to 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:

$(arr).map(function(key, val) { return (typeof(val) == "string") ? val.trim() : val; })

self-explanatory, isn't it?

Sign up to request clarification or add additional context in comments.

4 Comments

String.prototype.trim is not available in older browsers.
Sorry about that.. didn't know :S How older? Anyway, the second option should avoid that. Thanks for the remark.
It will definitely not work in IE8 and older. Here is a nice table: kangax.github.com/es5-compat-table
Wow, and I'm still complaining about IE6... The entire browser family seems to be a crap.
2

It appears you are using jQuery.

You could make a normalised copy of your Array with trimmed members.

arr = $.map(arr, $.trim);

(thanks Meouw and Felix.)

3 Comments

No need to wrap $.trim in a function, just pass it as a reference: arr = $.map(arr, $.trim );
You probably can just write: arr = $.map(arr, $.trim);
Oh, early in Australia..... very late here in Europe ;) Have a nice day then, I will go to sleep :D
1

You can try using the grep function.

jQuery.grep(arr, function(n, i){
    if (jQuery.trim(n) == 'John') $("span:eq(0)").text(i);
    return false;
});

Comments

0

Your array has " John", but you're checking for "John". These are obviously different strings. This works for me:

<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>

Or are you wanting to do some sort of fuzzy matching that ignores spaces?

Comments

0

Use a trimmed copy if you want to ignore spaces. Make sure to only trim strings, else your lookup of 4 will fail:

var arr = [ 4, "  Pete", 8, "  John" ];
var trimmedArr = jQuery.map(arr, function(item)
{
    return typeof item == "string" ? jQuery.trim(item) : item;
});

$("span:eq(0)").text(jQuery.inArray("John", trimmedArr));
$("span:eq(1)").text(jQuery.inArray(4, trimmedArr));
$("span:eq(2)").text(jQuery.inArray("Karl", trimmedArr));

This method works (jsFiddle).

Comments

0

try not using jQuery, instead iterate through the array manually and use a RegExp. Replace jQuery.inArray("John", arr) with inArray("John", arr), and put the following function in your script:

function inArray(string, arr){
    var re = RegExp(string, "g");
    for(var i in arr){
        if(arr[i].search(re) != -1){
            return i;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.