Skip to content

Instantly share code, notes, and snippets.

@alexpilugin
Last active July 10, 2017 08:07
Show Gist options
  • Save alexpilugin/d039e6719e355c967c223f245f122247 to your computer and use it in GitHub Desktop.
Save alexpilugin/d039e6719e355c967c223f245f122247 to your computer and use it in GitHub Desktop.
var obj = {
a: "a",
b: "b",
c: [0, 1, 2]
}
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key + ': ' + obj[key]);
}
}
//ES6:
Object.keys(obj).forEach(function(key,index) {
console.log(index + " " + key + ': ' + obj[key]);
});
//iterate-props:
function forEachProps(obj, action) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
action(obj[key]);
}
}
}
forEachProps({a:"a", b:"b"}, console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment