Skip to content

Instantly share code, notes, and snippets.

@granmoe
Last active April 16, 2018 02:03
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 granmoe/1d10f11c32a236e4d3c2350dd455731c to your computer and use it in GitHub Desktop.
Save granmoe/1d10f11c32a236e4d3c2350dd455731c to your computer and use it in GitHub Desktop.
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