数组和字符串的includes() 一招简写if判断条件

    // 数组使用
    let arr = [1, 5, 7]
    let a = 7
    let bool = arr.includes(a)
    console.log('bool: ', bool); // true a 等于 1、5、7 bool 都为true

    // 用法简写 一个元素多个值 if判断
    if (a === 1 || a === 5 || a === 7) {
      console.log(111111111111);
    }

    // 简写  
    if (arr.includes(a)) {
      console.log(111111111111);
    }
 
    // 字符串使用
    const str = '苏州abc56'
    const boolA = str.includes('ab')
    console.log('boolA: ', boolA); // true
    const boolB = str.includes('Ab')
    console.log('boolB: ', boolB); // false
 
 
    // 注意 includes 区分大小写
 
       // 本期上图 lsp们
 

 

 
 
posted @ 2022-09-16 14:12  牧羊少年&  阅读(107)  评论(0)    收藏  举报