工作中有时候会遇到这样的情况
var name = "love"
var name2 = "hate"
if(name=="a" || name=="b" || name=="c" || name=="d" ......){}
if(name2=="a" || name2=="b" || name2=="c" || name2=="d" ......){}
太笨重了,我们可以这样:
var name = "love"
var arr = ["a", "b", "c", "d", "e", "f"]
if (arr.includes(name)) {
console.log(1)
}
还一个用法:
var p1 = [];
for (var i in ones) {
var name = ones[i].name;
if (!p1.includes(name)) {
p1.push(name)
}
}
网友评论