多重判断使用Array.includes
以下判断,正常写法:
if (animal == 'cat' || animal== 'dog' || animal== 'pig' || animal== 'bat') { // ...if the value are too much, I'll be headache
console.log('Circle of friends like zoo');
}
使用Array.includes优化
function (who) {
var arr = ['cat', 'dog', 'bat', 'pig'];
if (arr.includes(who)) {
console.log('Circle of friends like zoo');
}
}
console.log(arr.includes('cat')); // expected output: true
console.log(arr.includes('at')); // expected output: false
网友评论