-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Description
Hi!
I wanted to do some quick scraping and found $(...).each(() => {...})
a bit uncomfortable now that we have for ... of
in ES6. Basically I'd like to do the following:
const cheerio = require('cheerio');
const $ = cheerio.load(myHtml);
for (const element of $('.someClass')) {
// ...
}
for (const [index, element] of $('.someOtherClass').entries()) {
// ...
}
I whipped up a quick patch that allows the above syntax:
const cheerio = require('cheerio');
cheerio.prototype[Symbol.iterator] = function* () {
for (let i = 0; i < this.length; i += 1) {
yield this[i];
}
};
cheerio.prototype.entries = function* () {
for (let i = 0; i < this.length; i += 1) {
yield [i, this[i]];
}
};
LMK if this is out of scope -- if not I'll whip up a PR.
chocolateboy, maple3142 and asix1337
Metadata
Metadata
Assignees
Labels
No labels