Created
May 8, 2019 18:34
-
-
Save AbeEstrada/158cf9856c2e26eb3f8e562237cb3696 to your computer and use it in GitHub Desktop.
Filter by object value, author @brunoandradebr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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