Aliendreamer

Filter distinct objects by property from array

Oct 1st, 2020
957
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // creates an object only once - garbage be glad ^^
  2. let cachedObject = {};
  3.  
  4. // array to be filtered
  5. let arr = [
  6.     {id : 0, prop : 'blah'},
  7.     {id : 1, prop : 'foo'},
  8.     {id : 0, prop : 'bar'}
  9. ]
  10.  
  11. // "filter" to object - keep original array - garbage be glad too ^^
  12. arr.map((item)=> cachedObject[item.id] = item)
  13.  
  14. // optional, turns object to array
  15. arr = Object.values(cachedObject)
RAW Paste Data