2d x/y to 1d index conversion (and vice-versa)

Mapping 2d x/y into a 1d array index and vice-versa

Convert 1d index to a 2d coordinates

// 1d array to 2d coords based on a given width
let width = 10;
for (let i = 0; i < 25; i++)
{
    let x = i % width;
    let y = Math.floor(i / width);
}

Convert 2d coordinates back to an index

// 2d coords to 1d array
let width = 25;
let height = 10;
for (let y = 0; y < height; y++)
{
    for (let x = 0; x < width; x++)
    {
        let i = y * width + x;
    }
}