First of all thanks for this work. I really like this library! 馃憦
By giving a try I notice something, given this code:
$html = cheerio.load(...) // Some html with a list that we can loop trough
$html.find('li').each(function (index, element) {
// ... Some code for the loop explained bellow
});
One: Arguments
First of all let's take a look at the callback that each uses.
function (index, element) {...}
The arguments are passed like the jQuery style and even if I know that this is meant by choice, I'm just wondering why? I'm also asking since now days map, filter, forEach they already invert those arguments, and I guess is kind of convention.
Two: Scoping
From the examples seems that I have to use the variable this to access the data of the element that I'm looping trough. What is the difference from the element passed trough the callback?
Could we just move in argument so we can relay in a more functional way and gain the usage of the so lovely arrow function () => {}? (I would love this 馃榿)
Then I get an error, but not sure what is related, seems I can't access to property like .html in the each loop. But I guess once I understand why I would dedicate a proper ticket for it.
Thanks again for the work done here!
馃檹
To access functions like .html() inside the loop you need to wrap the element like this $(el).html() or this $(this).html()
I also think it is confusing that the index is the first argument, my workaround is to use lodash collection methods on the resulting list of elements to avoid inconsistencies: _.map($('div'), el => $(el).html())
Not the prettiest but it works.
Hope this helps
_($('div')).map($).map($el => $el.html()).value()
Also does the job
Sure does help! Thanks @dbuezas !
But I was also purposing to fix it :-)
If is a good idea, although is a breaking change I can make a PR for it.
Not the kind of breaking change this module should adopt :/