Skip to content

Instantly share code, notes, and snippets.

@Aaronius
Last active September 13, 2019 21:15
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 Aaronius/8f400872a8e59fb3e554d0719ce57eb7 to your computer and use it in GitHub Desktop.
Save Aaronius/8f400872a8e59fb3e554d0719ce57eb7 to your computer and use it in GitHub Desktop.
Memoize
const getCommaDelimitedKeys = memoize(object => {
return Object.keys(object).join(",");
});
const fruits = {
apple: 95,
avocado: 234,
banana: 133
};
// apple,avocado,banana
console.log(getCommaDelimitedKeys(fruits));
console.log(getCommaDelimitedKeys(fruits));
const getCommaDelimitedKeys = memoize((...objects) => {
const allUniqueKeys = objects.reduce((allUniqueKeys, object) => {
Object.keys(objectKeys).forEach(key => {
if (allUniqueKeys.indexOf(key) === -1) {
allUniqueKeys.push(key);
}
});
return allUniqueKeys;
}, [])
return allUniqueKeys.join(",");
});
const getCommaDelimitedKeys = memoize((...objects) => {
const allUniqueKeys = objects.reduce((allUniqueKeys, object) => {
Object.keys(objectKeys).forEach(key => {
if (allUniqueKeys.indexOf(key) === -1) {
allUniqueKeys.push(key);
}
});
return allUniqueKeys;
}, [])
return allUniqueKeys.join(",");
}, (...objects) => {
return objects.reduce((allIds, object) => {
allIds.push(object.id);
return allIds;
}, []);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment