Last active
May 23, 2016 03:56
-
-
Save beaucharman/a0bdd5a2d22a03b217df0282a66a3821 to your computer and use it in GitHub Desktop.
A forEach for JavaScript objects
This file contains hidden or 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
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