Skip to content

Instantly share code, notes, and snippets.

@jlaguilargomez
Created March 15, 2021 07:35
Show Gist options
  • Save jlaguilargomez/f63e401c24d8341452fcecf382017f93 to your computer and use it in GitHub Desktop.
Save jlaguilargomez/f63e401c24d8341452fcecf382017f93 to your computer and use it in GitHub Desktop.
Implement recursivity to create a group of elements with N length from an original Array
const arr = [1, 2, 3, 4, 5, 6];
let newArr = [];
const spliceArray = (originalArr, newArr, groupLength) => {
newArr.push(originalArr.splice(0, groupLength));
return originalArr.length === 0
? newArr
: spliceArray(originalArr, newArr, groupLength);
};
newArr = spliceArray(arr, newArr, 1);
console.log(newArr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment