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