Guest User

Untitled

a guest
Jan 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. const arr = [
  2. {
  3. id: 10,
  4. title: "Test 10",
  5. item: "Item 1"
  6. },
  7. {
  8. id: 10,
  9. title: "Test 10",
  10. item: "Item 2"
  11. },
  12. {
  13. id: 11,
  14. title: "Test 11",
  15. item: "Item 1"
  16. },
  17. {
  18. id: 11,
  19. title: "Test 11",
  20. item: "Item 2"
  21. },
  22. {
  23. id: 11,
  24. title: "Test 11",
  25. item: "Item 3"
  26. },
  27. {
  28. id: 11,
  29. title: "Test 11",
  30. item: "Item 4"
  31. },
  32. ]
  33.  
  34. function convertArr(arr) {
  35. return arr
  36. .filter((e, i, a) =>
  37.  
  38. !a.slice(0, i).some(other => other.id === e.id)
  39. )
  40. .map(({id, title}) => ({
  41. id,
  42. title,
  43. items:
  44. arr
  45. .filter(el => el.id === id)
  46. .map(({item}) => ({item}))
  47. }))
  48. }
  49.  
  50. console.log(convertArr(arr))
Add Comment
Please, Sign In to add comment