-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 ?

sarah
  • 3,819
  • 4
  • 38
  • 80
  • have you tried anything? what does not work? do you have only one array with `true`? – Nina Scholz Jul 01 '20 at 06:53
  • Does this answer your question? [To find Index of Multidimensional Array in Javascript](https://stackoverflow.com/questions/16102263/to-find-index-of-multidimensional-array-in-javascript) – Heretic Monkey Jul 01 '20 at 16:24

4 Answers4

1

You just need add filter for this,

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

let result = myArray.filter(function(value) {
    return value[0]=== true;
});
Hasintha Janka
  • 1,608
  • 1
  • 14
  • 27
1
const arrays = [ [ false ], [ true ], [ false ] ] 

const index = [].concat.apply([], arrays).findIndex(value => value === true)
eemrah
  • 1,603
  • 3
  • 19
  • 37
0
const idx = myArray.findIndex(i => i[0]);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
see sharper
  • 11,505
  • 8
  • 46
  • 65
  • 2
    Post answer with only code is not good, please explains always your answer. – Simone Rossaini Jul 01 '20 at 13:49
  • 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. – Donald Duck Jul 01 '20 at 16:15
  • 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. – see sharper Jul 01 '20 at 23:42
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)
}

})

hamidreza nikoonia
  • 2,007
  • 20
  • 28