3

enter image description here

As shown in the image, I just do inArray on an array, looking for a node. and $previously_selected_node and the item at index 37 in $shapes are the same object.... so... why isn't it working?

EDIT: I found another way to search after one of the aswerers postedd his answer:

var result = -1;
jQuery.each(shapes, function(key, value){
    if (value.id == shape.id){
        result = key;
    }
});
return result;

apparently, part of my problem is that I can't return in the middle of a loop. (I was returning the instant a match was found, which was causing some issues.)

5
  • Does $shapes have length? Commented Sep 26, 2011 at 14:50
  • $shapes.length is undefined... so.. then it's not actually an array, even though I assign values to it like $shapes[id] = val; so... how do I search through it then? Commented Sep 26, 2011 at 14:52
  • Usually, when questions are of the form 'Why doesn't <insert name of popular library function> work?' come along - it is being used incorrectly. $shapes may not be an Array, also comparison of objects is prone to trouble. Commented Sep 26, 2011 at 14:53
  • So, how do I search through whatever object this is? Commented Sep 26, 2011 at 14:55
  • @TheLindyHop, using jQuery.each(), but, have a mind to how you compare elements. Commented Sep 26, 2011 at 14:58

2 Answers 2

3

Your object is not an array.
$.inArray only work on array-like objects with a length and a set of properties named 0 through length - 1.

You need to search your non-array manually.
For example, you could use a for / in loop to loop through all properties that actually exist and see if any of them match your object:

for (var key in $shapes) {
    if ($shapes[key] === yourObject) {
        //Match!
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

how? I've tried .each, for loops... not really sure what this object is either.
nice, I found a similar solution, added to OP
3

You're doing it the wrong way round. It's actually $.inArray(value, array).

And as others already stated: inArray is for arrays, not for objects.

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.