Skip to content

Instantly share code, notes, and snippets.

@iuliaL
Created March 6, 2018 14:29
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 iuliaL/03abfd3a9752a972ca03a06c72b54c4b to your computer and use it in GitHub Desktop.
Save iuliaL/03abfd3a9752a972ca03a06c72b54c4b to your computer and use it in GitHub Desktop.
function splitArrayInArraysBySection (arr) {
const returnsObject = arr.reduce((newArrSofar, item) => {
const toAdd = newArrSofar[item.section] ? [...newArrSofar[item.section], item] : [item]
return {
...newArrSofar,
[item.section]: toAdd
}
},{});
return Object.values(returnsObject); // to return array actually
}
const check = [
{id: 'a', section: 0},
{id: 'b', section: 1},
{id: 'c', section: 2},
{id: 'd', section: 1}
]
splitArrayInArraysBySection(check)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment