I'm trying to get the row and column index of an element from a 2D array using map
function.
Here's my code -
function getIndex() {
var values = [['a', 'f', 'k', 'p', 'u'], ['b', 'g', 'l', 'q', 'v'], ['c', 'h', 'm', 'r', 'w'], ['d', 'i', 'n', 's', 'x'], ['e', 'j', 'o', 't', 'y']];
var output = values.map(function (row, rowIndex) {
return row.map(function (col, colIndex) {
if (col == 's') {
return values[rowIndex][colIndex];
}
})
});
console.log(output);
}
getIndex();
And this is the output that I get when I run it at my end -
[[null, null, null, null, null], [null, null, null, null, null], [null, null, null, null, null], [null, null, null, s, null], [null, null, null, null, null]]
I don't intend to use a for
loop as that would not be optimal for the data set that I'm working on. Need to use either map
, reduce
or filter
functions in JavaScript Arrays. Please help!
Note: I only need the row and column Index for both and not the actual value.