通过以下内容,我能够找到每个单个值并从中删除类,但我想添加另一个条件,其中如果存在数组两个值,则执行某些操作,有人可以查看并给出一个想法
$.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) {
}
}
}
});
编辑:当前的输出是,它找到一个值并从中删除一个类,我想要的是,如果有 2 个值,则添加类。
最佳答案
您可以像这样计算数组中某个值的出现次数:
arr.reduce((count, el) => count + (el === value), 0)
然后您可以在 switch 中使用它:
const occurences = arr.reduce((count, el) => count + (el === value), 0);
switch(occurences) {
case 1:
alert("one");
case 2:
alert("two");
}
关于javascript - 当数组有两个值时如何处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49426858/