Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Last active November 21, 2018 06:23
Show Gist options
  • Save dmjcomdem/59decabcec5d8c90782a7e3ce3c28b9c to your computer and use it in GitHub Desktop.
Save dmjcomdem/59decabcec5d8c90782a7e3ce3c28b9c to your computer and use it in GitHub Desktop.
Sample find method
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