Created
March 6, 2018 14:29
-
-
Save iuliaL/03abfd3a9752a972ca03a06c72b54c4b 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 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