Last active
September 13, 2019 21:15
-
-
Save Aaronius/8f400872a8e59fb3e554d0719ce57eb7 to your computer and use it in GitHub Desktop.
Memoize
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
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)); |
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
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(","); | |
}); |
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
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