Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查数组中是否存在jQuery对象?_Javascript_Jquery_Arrays - Fatal编程技术网

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

Javascript 如何检查数组中是否存在jQuery对象?,javascript,jquery,arrays,Javascript,Jquery,Arrays,给定一个项和一个数组,我想知道项是否存在于数组中 item是一个jQuery对象,例如$(“.c”)。您可以假设item.length==1 array是jQuery对象的数组,例如[$(.a”),$(.b”)]。此数组中的每个项可以表示0、1或更多对象 下面是我如何实现这一点的想法:() 那怎么办 if(jQuery.inArray(some, array) === -1) { //process data if "some" is not in array } else { //proces

给定一个
和一个
数组
,我想知道
是否存在于
数组

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

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

下面是我如何实现这一点的想法:()

那怎么办

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

阅读此处:

根据Felix的建议:

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

$(selector1, selector2, ...)

然后它可以实现为:

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

它必须是一个数组吗?为什么不使用jQuery对象和?@Felix:我猜你的意思是使用
$(“.a.b”)
。听起来很合理!或者您可以使用来构建jQuery对象。虽然经过多次向上投票,但对于包含jQuery对象的数组(这是OP要求的)来说,这是不起作用的。请参阅Misha自己的答案,了解使用jQuery的.index()方法的正确解决方案。此解决方案不适用于jQuery元素数组。请详细说明一下?“试试这个”式的回答不会解释太多,而且会鼓励重复问同样的问题。同意上面的评论:详细说明为什么这是一个答案会对每个人都有帮助,特别是当你在一行中使用JavaScript的两个“技巧”时。好的解决方案!我不认为这真的需要一个函数包装器,虽然OP在它自己的函数中,但是在函数中只做一行就足够了。
if(jQuery.inArray(some, array) === -1)
{
//process data if "some" is not in array
}
else
{
//process if "some" is in array
}
$(selector1, selector2, ...)
$(selector1).add(selector2)...
function inArray(item, arr) {
  return (arr.index(item) != -1);
}
if($.inArray("valueYouWantToFind", nameOfTheArray) == true) {
  Your code;
 }

 Eg.,
 var userChoice = ["yes"];
 if($.inArray('yes',userChoice) == true) {
                    alert("found");
                }
console.log(!!~$.inArray("a", ["a", "b", "c"]));
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;