Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find index of NaN in a javascript array

[1, 2, 3].indexOf(3) => 2

[1, 2, NaN].indexOf(NaN) => -1

[1, NaN, 3].indexOf(NaN) => -1
like image 332
mcandre Avatar asked Mar 14 '11 03:03

mcandre


People also ask

How to check if array has NaN JavaScript?

Using isNaN() / Number.isNaN() isNaN() function only returns true if the value provided is actually NaN. So, the Number. isNaN() is a reliable way of checking for NaN over isNaN().

How to find index in an array JavaScript?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.

How use indexOf in JavaScript?

JavaScript String indexOf()The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

How to get the first index of an array in JavaScript?

Javascript array is a variable that holds multiple values at a time. The first and last elements are accessed using an index and the first value is accessed using index 0 and the last element can be accessed through length property which has one more value than the highest array index.


3 Answers

You can use Array.prototype.findIndex method to find out the index of NaN in an array

let index = [1,3,4,'hello',NaN,3].findIndex(Number.isNaN)
console.log(index)

You can use Array.prototype.includes to check if NaN is present in an array or not. It won't give you the index though !! It will return a boolean value. If NaN is present true will be returned, otherwise false will be returned

let isNaNPresent = [1,2,NaN,'ball'].includes(NaN)
console.log(isNaNPresent)

Don't use Array.prototype.indexOf

You can not use Array.Prototype.indexOf to find index of NaN inside an array.Because indexOf uses strict-equality-operator internally and NaN === NaN evaluates to false.So indexOf won't be able to detect NaN inside an array

[1,NaN,2].indexOf(NaN) // -1

Use Number.isNaN instead of isNaN :

Here i choose Number.isNaN over isNaN. Because isNaN treats string literal as NaN.On the other hand Number.isNaN treats only NaN literal as NaN

isNaN('hello world') // true
Number.isNaN('hello world') // false

Or, Write your own logic :

You can write your own logic to find NaN.As you already know that, NaN is the only value in javascript which is not equal to itself. That's the reason i suggested not to use Array.prototype.indexOf.

NaN === NaN // false

We can use this idea to write our own isNaN function.

[1,2,'str',NaN,5].findIndex(e=>e!=e) // 3
like image 118
AL-zami Avatar answered Oct 12 '22 02:10

AL-zami


NaN is defined not to be equal to anything (not even itself). See here: http://www.w3schools.com/jsref/jsref_isNaN.asp

like image 13
phooji Avatar answered Oct 12 '22 02:10

phooji


You have to look at each item to return an array of the indexes that are NaN values-

function findNaNs(arr){
    return arr.map(function(itm, i){
        if(isNaN(itm)) return i;
        return false;
    }).filter(function(itm){
        return itm;
    });
}

findNaNs([1, NaN, 3, 4, 'cat'/3])

//or to find the first one-

function firstNaN(arr){
    var i= 0, L= arr.length;
    while(i<L){
        if(isNaN(arr[i])) return i;
        ++i;
    }
    return -1;
}
like image 2
kennebec Avatar answered Oct 12 '22 02:10

kennebec