jQuery的:为什么不jQuery.inArray()工作? [英] jQuery: why doesn't jQuery.inArray() work?
问题描述
由于图所示,我只是做inArray阵列上,找一个节点。和$previously_selected_node和指数37 $形状的项目是同一对象....所以...为什么不工作?
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?
编辑:
我找到了另一种方式来搜索后aswerers之一postedd他的回答:
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.)
推荐答案
您的对象不是一个数组。结果 $。inArray
只能用长度
和一组上的阵列状物体的工作命名属性 0
到长度 - 1 的
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!
}
}
这篇关于jQuery的:为什么不jQuery.inArray()工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!