Created
March 2, 2023 15:52
-
-
Save kariudo/75cfb5fd66836f52f6e017b7a165caec to your computer and use it in GitHub Desktop.
Flat list to matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convert a flat list into a 2D Array. | |
* | |
* @param list Flat list. | |
* @param elementsPerSubArray Size of inner arrays. | |
* @returns 2D array of the given size.. | |
*/ | |
private listToMatrix<T>(list: T[], elementsPerSubArray: number): T[][] { | |
const matrix: T[][] = []; | |
let index; | |
let k; | |
for (index = 0, k = -1; index < list.length; index++) { | |
if (index % elementsPerSubArray === 0) { | |
k++; | |
matrix[k] = [] as T[]; | |
} | |
matrix[k].push(list[index] as T); | |
} | |
return matrix; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment