Skip to content

Instantly share code, notes, and snippets.

@irrationnelle
Created April 3, 2018 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irrationnelle/6770c73250a6f64e29399d89e2b5a73b to your computer and use it in GitHub Desktop.
Save irrationnelle/6770c73250a6f64e29399d89e2b5a73b to your computer and use it in GitHub Desktop.
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const twoDimensionArray2 = array.reduce((array, number, index) => {
const criteria = 4;
const arrayIndex = Math.floor(index / criteria);
if (!array[arrayIndex]) {
array[arrayIndex] = [];
}
array[arrayIndex] = [...array[arrayIndex], number];
return array;
}, []);
console.log(array);
console.log(twoDimensionArray2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment