Skip to content

Instantly share code, notes, and snippets.

@alexalannunes
Last active July 5, 2023 11:48
Show Gist options
  • Save alexalannunes/081f3d807cfd42fbb5f0233af88f4656 to your computer and use it in GitHub Desktop.
Save alexalannunes/081f3d807cfd42fbb5f0233af88f4656 to your computer and use it in GitHub Desktop.
divide array
const a = [1,2,3,4,5,6,7,8]
const newarr=[]
const cols = 4
const interactions = a.length/cols
let index = 0
console.log(interactions)
for (let i = 0; i < interactions;i++) {
const slice = a.slice(index, index + cols)
newarr.push(slice)
index += cols
}
newarr // [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment