Created
December 19, 2019 17:23
-
-
Save jq-87/066660aaae511b246ef833fbbab12407 to your computer and use it in GitHub Desktop.
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
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