I have some arrays as below and I want to know how to get the index of the first number by js?
Arr1=[null,null,null,null,null,...,343,959,543,..,252,null,null,...,null]
I have some arrays as below and I want to know how to get the index of the first number by js?
Arr1=[null,null,null,null,null,...,343,959,543,..,252,null,null,...,null]
You can use findIndex
:
Arr1.findIndex(value => value !== null)
You can loop through the array and check it's value:
const Arr1=[null,null,null,null,null,343,959,543,252,null,null,null];
let index = -1;
for(let i = 0; i < Arr1.length; i++)
{
if (Arr1[i] !== null)
{
index = i;
break;
}
}
console.log(index);