Last active
January 11, 2018 22:31
-
-
Save lubien/a8d8402a36ec84edef85360f814f53e3 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
const arr = [ | |
{ | |
id: 10, | |
title: "Test 10", | |
item: "Item 1" | |
}, | |
{ | |
id: 10, | |
title: "Test 10", | |
item: "Item 2" | |
}, | |
{ | |
id: 11, | |
title: "Test 11", | |
item: "Item 1" | |
}, | |
{ | |
id: 11, | |
title: "Test 11", | |
item: "Item 2" | |
}, | |
{ | |
id: 11, | |
title: "Test 11", | |
item: "Item 3" | |
}, | |
{ | |
id: 11, | |
title: "Test 11", | |
item: "Item 4" | |
}, | |
] | |
function convertArr(arr) { | |
return arr | |
.filter((e, i, a) => | |
!a.slice(0, i).some(other => other.id === e.id) | |
) | |
.map(({id, title}) => ({ | |
id, | |
title, | |
items: | |
arr | |
.filter(el => el.id === id) | |
.map(({item}) => ({item})) | |
})) | |
} | |
console.log(convertArr(arr)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment