【问题标题】:How to check if jQuery object exist in array?如何检查数组中是否存在jQuery对象?
【发布时间】:2012-02-04 20:13:40
【问题描述】:

给定一个item 和一个array,我想知道item 是否存在于array 中。

item 是一个 jQuery 对象,例如$(".c")。你可以假设item.length == 1。

array 是一个 jQuery 对象数组,例如[$(".a"), $(".b")]。此数组中的每个项目可能代表 0、1 或更多对象。

这是我想到的实现方式:(live demo here)

function inArray(item, arr) {
    for (var i = 0; i < arr.length; i++) {
        var items = $.makeArray(arr[i]);

        for (var k = 0; k < items.length; k++) {
            if (items[k] == item[0]) {
                return true;
            }
        }
    }

    return false;
}

你能找到更优雅的实现吗?


示例:

HTML:

<div class="a">Hello</div>
<div class="a">Stack</div>
<div class="a">Overflow</div>

<div class="b">Have</div>
<div class="b">a</div>
<div class="b">nice</div>
<div class="b">day!</div>

<div class="c">Bye bye</div>

JS:

console.log(inArray($(".a").eq(2), [$(".a"), $(".b")])); // true
console.log(inArray($(".b").eq(3), [$(".a"), $(".b")])); // true
console.log(inArray($(".c"), [$(".a"), $(".b")]));       // false
console.log(inArray($(".a").eq(2), [$(".b")]));          // false
console.log(inArray($(".a").eq(2), []));                 // false
console.log(inArray($(".c"), [$("div")]));               // true

【问题讨论】:

  • 必须是数组吗?为什么不使用 jQuery 对象和.index()?
  • @Felix:我猜你的意思是使用$(".a, .b")。听起来很合理!
  • 或者你可以使用add()来构建jQuery对象。

标签: javascript jquery arrays


【解决方案1】:

怎么样

if(jQuery.inArray(some, array) === -1)
{
//process data if "some" is not in array
}
else
{
//process if "some" is in array
}

在这里阅读:http://api.jquery.com/jQuery.inArray/

【讨论】:

  • 虽然被多次投票,但这不适用于包含 jQuery 对象的数组(这是 OP 要求的)。使用 jQuery 的 .index() 方法查看 Misha 自己的答案以获得正确的解决方案。
  • 此解决方案不适用于 jquery 元素数组
【解决方案2】:

根据菲利克斯的建议:

[$(selector1), $(selector2), ... ]可以简化为

$(selector1, selector2, ...)

或

$(selector1).add(selector2)...

然后可以实现为:

function inArray(item, arr) {
  return (arr.index(item) != -1);
}

Live demo here

【讨论】:

  • 很好的解决方案!不过,我不认为这真的需要一个函数包装器——虽然 OP 在它自己的函数中,但只需在函数内部做一行就足够了。
【解决方案3】:
if($.inArray("valueYouWantToFind", nameOfTheArray) == true) {
  Your code;
 }

 Eg.,
 var userChoice = ["yes"];
 if($.inArray('yes',userChoice) == true) {
                    alert("found");
                }

【讨论】:

    【解决方案4】:
    console.log(!!~$.inArray("a", ["a", "b", "c"]));
    

    【讨论】:

    • 您能详细说明一下吗? “试试这个”风格的答案没有太多解释,而是鼓励人们一遍又一遍地问同样的问题。
    • 同意上面的评论:详细说明为什么这是一个答案将对每个人都有帮助,尤其是当您在一行中使用 JavaScript 的两个“技巧”时。
    【解决方案5】:
    data = [
      {val:'xxx',txt:'yyy'},
      {val:'yyy',txt:'aaa'},
      {val:'bbb',txt:'ccc'}
    ];
    
                var dummyArray = [];
                var distinctValueArray = [];
    
                $.each(data, function (index, firstobject) {
                  //push first element of object in both dummy array and distinct array.
    
                   if (index == 0) {
                        distinctValueArray.push(firstobject);
                        dummyArray.push(firstobject.txt);
                    }
                    else {
    
                        //started from 2nd index.
    
                       if ($.inArray(firstobject.txt, dummyArray) == -1) {
                            distinctValueArray.push(firstobject);
                        }
                        dummyArray.push(firstobject.txt);
                    }
                });
                dummyArray.length=0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2020-07-11
      • 1970-01-01
      • 2017-06-22
      相关资源
      最近更新 更多