5 ms·
There's a huge gotcha: _.map, _.forech, ... can iterate over objects (often used as associative arrays) - Native map can't. At some point you'll need lodash an
by frostik 10y ago
There's a huge gotcha:
_.map, _.forech, ... can iterate over objects (often used as associative arrays) - Native map can't.
At some point you'll need lodash and co again - at least for convinience.
- DrJokepu 10y agoObject.keys() is rather well supported these days though.
- frostik 10y agoYes, but means that you need to implement map, foreach, filter yourself. So you might be better off with lodash.
- deleted 10y ago[deleted]
- mullsork 10y agoI think he's referring to `Object.keys(object).reduce`. We polyfill `Object.entries` so we do `Object.entries(object).reduce((result, [key, value]) => ...)`
- crucialfelix 10y agoRelated: for (var key of Object.keys(imps)) { } I figured that would work with babel-2015 and it did work on Chrome and Safari but on Firefox visitors were getting: Symbol.Iterator undefined You need http://babeljs.io/docs/plugins/transform-es2015-for-of/ http://babeljs.io/docs/plugins/transform-es2015-for-of/ as well. So there is much less risk in using _.each(imps, (value, key) => and not having to discover what you don't know. I also added Firefox to the unit tests after that.
- jacobr 10y agoAnd Object.values is supported in Chrome and Firefox, and has polyfills for other browsers.
- avolcano 10y agoYup, this is my biggest issue with JavaScript's built-in functional utilities. It just totally ignores treating objects like dictionaries/maps. Even ES6 Maps/Sets lack functional features. There's a strawman proposal out there to solve this but until then I'll be sticking with Lodash: https://github.com/leebyron/ecmascript-iterator-hof https://github.com/leebyron/ecmascript-iterator-hof
- frenchie4111 10y agoI agree, this is the sole reason I use atleast underscore in every project.
- jkd3v 10y agoFurthermore, lodash/underscore contain some null object patterns: null.map(...) will throw an error, while _.map(null, ...) returns []
- connorjburton 10y agoI think this is what makes Lodash so convenient, I just want expected functions like _.forEach to work with whatever I throw at it.
- jdd 10y agoLodash is helping with JavaScript language additions like https://github.com/leobalter/object-enumerables https://github.com/leobalter/object-enumerables https://github.com/bterlson/proposal-flatMap https://github.com/bterlson/proposal-flatMap
- swang 10y agothe repo already points that out. >Important: Note that the native equivalents are array methods, and will not work with objects. If this functionality is needed, then Lodash/Underscore is the better option.
- runarberg 10y agoWe already have Object.keys()[1], and Object.values()[2] and Object.entries()[3] are in the stage 4 proposal[4] and should be safe to polyfill. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [4]: https://github.com/tc39/proposal-object-values-entries https://github.com/tc39/proposal-object-values-entries