分享
三行代码  ›  专栏  ›  技术社区  ›  Zaki Kazmi

数组。某些数组必须至少包含一个填充值

  •  0
  • Zaki Kazmi  · 技术社区  · 5 天前

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

    基本上,只要数组包含填充的字符串值和一些空字符串值,我就会尝试这样做。例如,只要数组中有一个填充的字符串值,我就希望它是真的。

    const array1 = [ 'g', '']; //returns true
    const array2 = [ '', '']; //return true
    const array3 = [ '', 'x']; //return true
    const array4 = [ 'g', 'x']; //return false
    // checks whether an element contains at least one populated string (non blank string)
    const even = (element) => element === '';
    
    console.log(array1.some(even)); //Should be True
    console.log(array2.some(even)); //Should be False
    console.log(array3.some(even)); //Should be True
    console.log(array4.some(even)); //Should be True
    

    1 回复  |  直到 5 天前
        1
  •  1
  •   CertainPerformance    5 天前

    反转测试-检查某些元素是否正确

    const array1 = [ 'g', '']; //returns true
    const array2 = [ '', '']; //return true
    const array3 = [ '', 'x']; //return true
    const array4 = [ 'g', 'x']; //return false
    // checks whether an element contains at least one populated string (non blank string)
    const even = (element) => element !== '';
    
    console.log(array1.some(even)); //Should be True
    console.log(array2.some(even)); //Should be False
    console.log(array3.some(even)); //Should be True
    console.log(array4.some(even)); //Should be True