To ensure your code runs smoothly even on Jurassic-era browsers π¦, enter the world of polyfills:
Polyfill for Array.prototype.reduce:
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(callback /*, initialValue*/) {
// Polyfill for reduce: Because every browser deserves to reduce() π };
}
Polyfill for Object.keys:
if (!Object.keys) {
Object.keys = function(obj) {
// Polyfill for Object.keys: Opening locked doors πͺ };
}
Taking reduce to the next level
See how reduce can do wonders by switching keys and values in an array of objects:
Numeric keys in objects are like wild horses - free from the orderliness of array indices. They are strings and march to the beat of their own drum, sorting entries in the order they were added to the object.