Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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))
Add Comment
Please, Sign In to add comment