3 ms·
Couldn't this be done with a simple map function? Not sure the exact javascript but in ruby it would be as simple as: favourite_drinks = authors.map { |a| a[
by alinajaf 15y ago
Couldn't this be done with a simple map function? Not sure the exact javascript but in ruby it would be as simple as:
favourite_drinks = authors.map { |a| a[:favourite_drink] }
Internally I'd imagine the language will have to loop through the json anyway. Are there any other benefits I'm missing (apart from of course the nice css-like syntax, which is perfectly valid).
- Groxx 15y agoIt could be, yes. `map` isn't built-in on all browsers, though, which I think has hurt its use. i.e., most people don't think about it, so it doesn't get used as often.
- subbu 15y agoUnderscore.js and Prototype.js both define this function. You can include this and use map. Underscore delegates it to ECMAScript 5's native map if available.
- Groxx 15y agoIt's definitely 'out there', and you can always just copy-paste to get the same code and delegation[1], but the lack of default availability is a pretty damaging blow. It shouldn't be, but the Javascript world isn't exactly a pantheon of high-quality code. [1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map https://developer.mozilla.org/en/JavaScript/Reference/Global...