-1

I have an array like this

const myArray = [ [ false ], [ true ], [ false ] ] 

i want to get index of an element that has value == true from array above.

so because the true from array above is in the second element, then I want to get 1 as the index result

how to do that ?

2

4 Answers 4

1

You just need add filter for this,

const myArray = [ [ false ], [ true ], [ false ] ] 

let result = myArray.filter(function(value) {
    return value[0]=== true;
});
Sign up to request clarification or add additional context in comments.

1 Comment

That returns a filtered array, not the index of the array with a true value.
1
const arrays = [ [ false ], [ true ], [ false ] ] 

const index = [].concat.apply([], arrays).findIndex(value => value === true)

Comments

0

you can loop on your array with map method


const arr = [ [ false ], [ true ], [ false ] ] 

arr.map((item, index) => {

if (item[0]) {
  console.log(index)
}

})

1 Comment

map returns a new array. if this is not used later, it makes no sense.
0
const idx = myArray.findIndex(i => i[0]);

3 Comments

Post answer with only code is not good, please explains always your answer.
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
The code is so simple I would think it self explanatory, noting that the other answers either offer no explanation, or a rather redundant one-liner. IMO this is still the best answer. The accepted answer is plain wrong, and the other upvoted answer uses a hard to read array flattening technique that does need explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.