Skip to content

Instantly share code, notes, and snippets.

@jq-87
Created December 19, 2019 17:23
Show Gist options
  • Save jq-87/066660aaae511b246ef833fbbab12407 to your computer and use it in GitHub Desktop.
Save jq-87/066660aaae511b246ef833fbbab12407 to your computer and use it in GitHub Desktop.
function wrapChildrenInSets(parentNode = null, setsOf = 2) {
if (!parentNode) return;
const children = [...parentNode.children];
const divSets = [];
const divsPerSet = setsOf;
const numSets = Math.ceil(children.length / divsPerSet);
for (let index = 0; index < numSets; index++) {
const set = [...children].splice(index * divsPerSet, divsPerSet);
const setDiv = document.createElement('div');
set.forEach(item => {
setDiv.appendChild(item);
});
divSets.push(setDiv);
}
divSets.forEach(set => {
parentNode.appendChild(set);
});
}
export default wrapChildrenInSets;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment