js找出数组里的NaN


方法一:使用indexOf是不能找到数组里的NaN的 ,正确用法是,includes

  var arr10 = [1, NaN, 2]
         console.log(arr10.indexOf(NaN)); // -1  用这个方法找数组里的NaN是找不到的

        // includes找出数组里的NaN
        console.log(arr10.includes(NaN)); // true


 方法二:使用find

   let a = [1,2,3,NaN,3,NaN]
            let b =   a.find(v=>Object.is(v,NaN))
            console.log(b);

方法三:使用findIndex

  let a = [1,2,3,NaN,3,NaN]
           let c =  a.findIndex(v=>Object.is(v,NaN))
           console.log(c);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM