Hi there,
It looks like there is some bug in processing arrow functions inside of a .each() iteration for a cheerio object.
E.g.
This will not work:
elements.each((i, element) => {
console.log($(this).text());
});
Whereas, this works:
elements.each( function (i, element) {
console.log($(this).text());
});
Anyone have any feedback on this? If this is a known issue I apologize, I'm relatively new to the cheerio community :)
Hi there! The behavior you have reported is not directly related to Cheerio. Instead, it concerns the JavaScript language itself. Unlike "traditional" functions such as those defined with the function keyword, so-called "arrow functions" have a this value which is lexically-scoped. You can learn more about arrow functions at this page on the Mozilla Developer Network.
@jugglinmike is there a way to access the this value that Cheerio is using in the "traditional" function, while still using the "arrow function"?
@trevorfrese
Would element be a reference to this?
e.g.,
elements.each((i, element) => { console.log($(element).text()); });
@gforceg that works!
Most helpful comment
@trevorfrese
Would
elementbe a reference tothis?e.g.,