简体   繁体   English

jquery.inArray()函数不起作用

[英]jquery.inArray() function not working

I am having a array as follows 我有如下数组

       var nameIDHashMap = [];
       nameIDHashMap.push({
            key: name,
            value: xmlLength
       });
     startToEnd.push({
          key: $(this).attr("startFrom"),
          value: $(this).attr("endTo")
    });

I m trying to use the inArray() function like shown below 我正在尝试使用如下所示的inArray()函数

            var variablestart = startToEnd[0].key;
            alert("The variable Start is :"+variablestart);
            var toEnd;
            if(jQuery.inArray(variablestart,nameIDHashMap) > -1) {
                alert('found');
            }
            if ($.inArray(variablestart, nameIDHashMap) != -1) 
            {
                alert("Found");
            //  toEnd = startToEnd[connectWindow].value
            }
            else
                alert("Fail");

I dont know why always the else loop is called. 我不知道为什么总是调用else循环。 None of the if loop is getting called. 没有if循环被调用。 Both of the array has that same key present. 两个数组都具有相同的密钥。 Please let me know where I am doing wrong.Thanks! 请让我知道我在哪里做错了。谢谢!

variablestart is a property of an element in the array, not an element in the array. variablestart是数组中元素的属性,而不是数组中元素。

var nameIDHashMap = [];
nameIDHashMap.push({
    key: 'foo',
    value: 'bar'
});

$.inArray(nameIDHashMap[0].key, nameIDHashMap); // this is not an element, -1
$.inArray(nameIDHashMap[0], nameIDHashMap); // this is an element, 0

You are essentially trying to equate the object { key: 'foo', value: 'bar' } to the string 'foo' , which are not equal. 本质上,您试图将对象{ key: 'foo', value: 'bar' }等同于字符串'foo' ,这是不相等的。

http://jsfiddle.net/jbabey/kgYSe/ http://jsfiddle.net/jbabey/kgYSe/

That's not how .inArray() works. 那不是.inArray()工作方式。 It searches for an array element that's equal to the value you pass in. It doesn't have any provisions for a comparison function. 它搜索等于您传递的值的数组元素。它没有任何用于比较函数的规定。

Even if it did work, what you're assembling there isn't a "hash table". 即使它确实起作用,您要组装的东西也没有“哈希表”。 If you want to do efficient lookups by key, you can just create named properties on a simple object: 如果要通过键进行有效的查找,则可以在一个简单的对象上创建命名属性:

var map = {};
map.someKey = someValue;

The .inArray() method and anything like it performs a linear-time search through the array, and that's not a very efficient way to do things if you're going to have an "interesting" number of key/value pairs. .inArray()方法以及类似的方法在整个数组中执行线性时间搜索,如果您要获得“有趣的”数量的键/值对,那不是一种非常有效的处理方法。

edit — if you really must keep a linear unindexed list of named properties, you could use a lookup function like this: 编辑 -如果您确实必须保留线性的未索引命名属性列表,则可以使用如下查找函数:

function find( list, key, test ) {
  test = test || function(e) { return e ? e.key == key : false; };

  for (var i = 0; i < list.length; ++i)
    if (test(list[i])) return i;
  return -1;
}

To use that, you'd just do: 要使用它,您只需执行以下操作:

if (find(nameIDHashMap, someKey) >= 0) {
  alert("Found!");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM