Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbeEstrada/158cf9856c2e26eb3f8e562237cb3696 to your computer and use it in GitHub Desktop.
Save AbeEstrada/158cf9856c2e26eb3f8e562237cb3696 to your computer and use it in GitHub Desktop.
Filter by object value, author @brunoandradebr
// creates an object only once - garbage be glad ^^
let cachedObject = {};
// array to be filtered
let arr = [
{id : 0, prop : 'blah'},
{id : 1, prop : 'foo'},
{id : 0, prop : 'bar'}
]
// "filter" to object - keep original array - garbage be glad too ^^
arr.map((item)=> cachedObject[item.id] = item)
// optional, turns object to array
arr = Object.values(cachedObject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment