Skip to content

Instantly share code, notes, and snippets.

@beaucharman
Last active May 23, 2016 03:56
Show Gist options
  • Save beaucharman/a0bdd5a2d22a03b217df0282a66a3821 to your computer and use it in GitHub Desktop.
Save beaucharman/a0bdd5a2d22a03b217df0282a66a3821 to your computer and use it in GitHub Desktop.
A forEach for JavaScript objects
function forEach(obj, callback) {
Object.keys(obj).forEach((item) => {
return callback(obj, item)
})
}
// usage
let obj = { a: 1, b: 2, c: 3 }
let array = []
forEach(obj, function(obj, item) {
array.push(obj[item])
})
console.log(array)
// > [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment