-2

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]


2
  • 1
    How do I ask and answer homework questions? Commented Aug 20, 2022 at 15:20
  • @Andreas, the question is how to find an index, not a value in the array. The provided "answered topic" shows how to find a value. This might be a duplicate of existing question, but definitely not the one provided as duplicate of. Commented Aug 20, 2022 at 15:26

2 Answers 2

4

You can use findIndex:

Arr1.findIndex(value => value !== null)
Sign up to request clarification or add additional context in comments.

1 Comment

That's an obvious duplicate (and a question without any effort, and - imho - most likely homework). So why the answer instead of a close-vote as dupe?
1

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.