Javascript 如何检查数组中是否存在jQuery对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8769022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:25:31  来源:igfitidea点击:

How to check if jQuery object exist in array?

javascriptjqueryarrays

提问by Misha Moroshko

Given an itemand an array, I would like to know if itemexist in array.

给定一个itemarray,我想知道是否item存在array

itemis a jQuery object, e.g. $(".c"). You can assume that item.length == 1.

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

arrayis an array of jQuery objects, e.g. [$(".a"), $(".b")]. Each item in this array may represent 0, 1, or more objects.

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

Here is how I thought to implement this: (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;
}

Can you find a more elegant implementation?

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



Example:

例子:

HTML:

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:

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

回答by Misha Moroshko

According to Felix's suggestion:

根据菲利克斯的建议:

[$(selector1), $(selector2), ... ]can be simplified to

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

$(selector1, selector2, ...)

or

或者

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

and then it can be implemented as:

然后它可以实现为:

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

Live demo here

现场演示在这里

回答by Snake Eyes

what about

关于什么

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

read here : http://api.jquery.com/jQuery.inArray/

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

回答by Umesh Patil

if($.inArray("valueYouWantToFind", nameOfTheArray) == true) {
  Your code;
 }

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

回答by Kirill Popov

console.log(!!~$.inArray("a", ["a", "b", "c"]));

回答by Manjur Gani

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;