4 ms·
Not mentioned here, but worth considering: Lodash's forEach works on "array-likes" not just proper Arrays. It works on `arguments`, it works on strings (iterat
by venning 8y ago
Not mentioned here, but worth considering: Lodash's forEach works on "array-likes" not just proper Arrays.
It works on `arguments`, it works on strings (iterating through characters), it works on objects (passing the key as the iteratee's second parameter in place of an index value), it works on HTMLCollection pseudo-arrays. It also doesn't throw on null or undefined.
- devmunchies 8y agoCan it work in a box? Does it work with a Fox?
- ybrah 8y agoyes, if a box and fox are iterables
- tedeh 8y agoA simple Object.keys(str|obj|etc...) call on your iterating object makes the other functions work on those data types too. Lodash may be fast, but recently I've been avoiding the basic "lodash function with native js equivalent" for one particular reason: stepping into js native functions when debugging (node inspect) is a breeze, and a complete nightmare when using lodash.
- venning 8y agoNo. Object.keys() does not allow iteration on strings or `arguments` or HTMLCollections. It only returns an array of indices not values. You can call Array#forEach() on the resultant array, but you'd need another iteratee signature to process them than you would for a proper forEach or for Lodash's method, or you'd have to dereference each element manually. At that point, you might as well run a for loop, since the code will be nearly identical.
- deleted 8y ago[deleted]
- TheAceOfHearts 8y agoAs of ES2015 you can convert an iterable like HTMLCollection into a regular array by using the spread syntax: [...iterable]
- pietro 8y agoThat, or `Array.from`. You can even create an array from a string: Array.from('string') [...'string']
- venning 8y agoYou have always been able to convert them with: Array.prototype.slice.call(htmlCollection); Which has been the canonical way to cast them. But it will still throw on null or undefined.