Last active
November 21, 2018 06:23
-
-
Save dmjcomdem/59decabcec5d8c90782a7e3ce3c28b9c to your computer and use it in GitHub Desktop.
Sample find method
This file contains hidden or 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
let moList = [{id: 1, name: 'NameMO-1'}, {id: 2, name: 'NameMO-2'}, {id: 3, name: 'NameMO-3'}]; | |
let find = (() => { | |
let store = {}; | |
return (list, id, key = 'id') => { | |
if (store[id]) { | |
return store[id]; | |
} else { | |
let item = list.find(item => item[key] === id); | |
return item ? (store[id] = item, store[id]) : undefined; | |
} | |
}; | |
})(); | |
find(moList, 3) // {id: 3, name: 'NameMO-3'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment