Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

var obj = {...}

// native JavaScript

Object.keys(obj).forEach(function(key){ console.log('obj.', key, ' = ', obj[key]) })

// Lodash

_.forEach(obj, function(value, key) { console.log('obj.', key, ' = ', value) })

Object.keys(obj) creates an array that contains the keys of the object, which makes it pretty trivial to call filter, map, reduce, etc. And getting the value from an object's key is also pretty straightforward...



Ruby: obj.each{|k,v| ...}

Sure you can do it in JS, but calling Object.keys then forEach (in whose closure you'll still have to reference obj[key] by the way) still doesn't feel optimal.


In which case, if you really can't live without it, just attach it to the prototype.

Object.prototype.each = function(k,v){ //... }


I would argue that automatically adding methods to a user-created object isn't optimal either.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: