Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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 数组有两个值时如何处理_Javascript_Jquery_Arrays_Loops - Fatal编程技术网

Javascript 数组有两个值时如何处理

Javascript 数组有两个值时如何处理,javascript,jquery,arrays,loops,Javascript,Jquery,Arrays,Loops,通过下面的内容,我可以找到每个值并从中删除类,但是我想添加另一个条件,如果数组中有两个值,那么可以做些什么,有人可以回顾一下并给出一个想法吗 $.each($target, function(index, htmlEle) { if ($(htmlEle).data("testest") != undefined) { var arr = $(htmlEle).data("testest").split(" "); for (var i = 0; i &l

通过下面的内容,我可以找到每个值并从中删除类,但是我想添加另一个条件,如果数组中有两个值,那么可以做些什么,有人可以回顾一下并给出一个想法吗

$.each($target, function(index, htmlEle) {
    if ($(htmlEle).data("testest") != undefined) {
        var arr = $(htmlEle).data("testest").split(" ");
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == value) {
                $(htmlEle).removeClass("hide");
            } else(arr[i] == value) {

            }

        }
    }

});
$。每个($target,function(index,htmlEle){
if($(htmlEle).data(“testest”)!=未定义){
var arr=$(htmlEle).data(“testest”).split(“”);
对于(变量i=0;i

编辑:当前的输出是,查找一个值并从中删除一个类,我想要的是,如果有两个值,则添加类。

您可以在如下数组中计算值的出现次数:

 arr.reduce((count, el) => count + (el === value), 0)
然后,您可以在交换机中使用它:

 const occurences = arr.reduce((count, el) => count + (el === value), 0);

 switch(occurences) {
   case 1:
    alert("one");
   case 2:
    alert("two");
}

可以按如下方式计算数组中某个值的出现次数:

 arr.reduce((count, el) => count + (el === value), 0)
然后,您可以在交换机中使用它:

 const occurences = arr.reduce((count, el) => count + (el === value), 0);

 switch(occurences) {
   case 1:
    alert("one");
   case 2:
    alert("two");
}

如果数组中有两个值,那么就做些什么,
->请描述它?上面更新了,请检查您能否向我们展示
arr
的样本数据?那个么问题就更清楚了。在您的问题
中添加,如果数组中有两个值,那么请做些什么,
->请描述它?更新如上,请检查您能否向我们展示
arr
的样本数据?那个么问题就更清楚了。在你的问题中加上这个