Skip to content

Instantly share code, notes, and snippets.

@megabayt
Last active April 27, 2022 16:27
Show Gist options
  • Save megabayt/dbb677fa9ab6ef779f15d6426716d0a6 to your computer and use it in GitHub Desktop.
Save megabayt/dbb677fa9ab6ef779f15d6426716d0a6 to your computer and use it in GitHub Desktop.
May be useful if you need to save empty values
const m = 4;
const n = 3;
const matrix = [
[1, 2],
[3, 4, 5],
[6, 7, 8, 9],
];
const arr = [];
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
arr[m * i + j] = matrix[i][j];
}
}
console.log(arr); // [1, 2, undefined, undefined, 3, 4, 5, undefined, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment