Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: why doesn't jQuery.inArray() work?

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

like image 662
NullVoxPopuli Avatar asked Feb 13 '26 20:02

NullVoxPopuli


1 Answers

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!
    }
}
like image 143
SLaks Avatar answered Feb 16 '26 10:02

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!