0

I have this code below. I don't know why inArray() doesn't output 0. Any idea?

var client = new Array();

function removeClient(id){

  alert(id);    //prints 17
  alert(client);   //prints 17
  alert(typeof(id));  //this prints "number"
  alert(typeof(client));  //this prints "object"
  alert($.inArray(id, client));   //this prints "-1", why?

}

Regards

Javi

6
  • 3
    How do you populate the array? Commented Mar 18, 2011 at 13:35
  • 2
    It works: jsfiddle.net/simevidas/PFU9x Commented Mar 18, 2011 at 13:39
  • 2
    Or here: jsfiddle.net/fkling/ub6xX Commented Mar 18, 2011 at 13:41
  • I'm populating the array using push(). If I add more elements the elements are separated with the comma in the output. I'm populating this way "client.push(data[1]);" Commented Mar 18, 2011 at 13:43
  • 1
    @user248959: Then data[1] is probably a string. Commented Mar 18, 2011 at 13:48

6 Answers 6

5

You probably populated the array with the string '17' and not the number 17. That's why it returns -1.

Live demo: http://jsfiddle.net/simevidas/s4Q3K/

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

1 Comment

@Felix Good riddle though :)
3

$.inArray returns -1 when an element is not found. Can we see where you are filling client with array values? You might not be filling the array correctly.

alert(client) should not print '17' it should print the array values separated by commas.

EDIT: I figured out what may the the issue. If client contains the string '17' and not the number 17, because 17 !== '17'

Example: http://jsfiddle.net/ub6xX/1/

Working example: http://jsfiddle.net/fkling/ub6xX/

2 Comments

If the array only contains one value, 17, the output it indeed only 17.
@Felix: I... knew that. I need more coffee.
2

Because inArray(id, client) checks whether id is in the array client.

And since id is not in that array (at least not per your sample), it returns -1. Know your API.

5 Comments

@Tomalak "id is not that array"? You mean "id is not in that array"?
@Tomalak If id (which is the number 17) is not in the array client then why does client print to '17'?
@Šime Of course. Fixed, thanks. I suppose that client is not actually an array in the OP's real code. It is in the sample, though. (If it was an array, it would print [17], and the sample would work.)
@Tomalak: It never prints the square brackets if you alert an array.
@Felix: Whoops, that's right. I was thinking console.log(). Nevertheless, it prints -1 because either client is not actually an array or it does not contain 17. It's a simple as that.
0

-1 means not found, so basically the id does not exist in the items of the array.

0 would mean that it had found the id at the first position of the array. Nothing in your code suggests that this should happen.

http://api.jquery.com/jQuery.inArray/

1 Comment

alert(client); indicates that the array contains one element.
0

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

Comments

0

I don't see where you you are putting the id into the client array in your exmaple I'm guessing because you don't have it in there in array can't find it

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.