DEV Community

Discussion on: Finding the coordinates of an element in a 2D array

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Nice! This might be slightly faster as it uses the internal indexOf function:

function find2dCoordinates(grid, number) {
    let output = null;
    grid.some((row, y)=> {
        const x = row.indexOf(number);
        if(x >= 0) {
           output = {x,y}
           return true;
        }
        return false;
    });
    return output;
}
Enter fullscreen mode Exit fullscreen mode