-
-
Save granmoe/1d10f11c32a236e4d3c2350dd455731c to your computer and use it in GitHub Desktop.
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
let arr = [10, 2, 4] | |
// product | |
arr.reduce((a, b) => a * b) // 80 | |
arr = [false, true, true] | |
// all | |
arr.reduce((a, b) => a && b) // false | |
// any | |
arr.reduce((a, b) => a || b) // true | |
// length (obviously, just use the length property instead in real code) | |
arr.reduce((a) => a + 1, 0) // 3 | |
// reverse | |
arr.reduce((a, b) => [b, ...a], []) // [true, true, false] | |
// add a prefix to all the first child key names of an object | |
const addPrefix = (prefix, obj) => | |
Object.entries(obj).reduce((result, [key, val]) => { | |
result[prefix + key] = val | |
return result | |
}, {}) | |
// addPrefix usage | |
const myObj = { a: 1, b: 2 } | |
addPrefix('hidy-ho-errybody__', myObj) | |
// returns { hidy-ho-errybody__a: 1, hidy-ho-errybody__b: 2 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment