Skip to content

Instantly share code, notes, and snippets.

@lubien
Last active January 11, 2018 22:31
Show Gist options
  • Save lubien/a8d8402a36ec84edef85360f814f53e3 to your computer and use it in GitHub Desktop.
Save lubien/a8d8402a36ec84edef85360f814f53e3 to your computer and use it in GitHub Desktop.
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