You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Both when using jquery and cheerio, I find myself quite often wanting to create a jquery/cheerio object from an element. Usually this happens when dealing with a list of elements, for example when iterating over them using a for loop (to be able to use continue, break and return, which is not easy to do with .each()) or when mapping a set of elements to an array. A jquery/cheerio object somewhat resembles an array of elements, so when iterating over it, I need to convert each element to its own jquery/cheerio object in order to be able to use the jquery/cheerio API on it. Here is a real-world example with jquery:
The conversion from the element to the jquery object happens by doing const $p = $(p);. In cheerio, this works the same, but $ is the root object of the document, rather than a global variable. This means that in a reusable function as the one above, I don't automatically have access to it. I can see 4 options how to do this. I'm not really happy with any of these, so I'm wondering if there is a better/recommended way how to do this.
In this case I'm basically passing the $ root object around as an additional parameter. I find it quite cumbersome to declare and pass the additional parameter on every function.
2. Use a custom helper function to convert a cheerio object to an array or iterator:
This seems to be the way how cheerio constructs these objects internally. It's not very elegant, as the method is not public and does not exist in the TypeScript typings.
4. Construct the cheerio object with a different root:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Both when using jquery and cheerio, I find myself quite often wanting to create a jquery/cheerio object from an element. Usually this happens when dealing with a list of elements, for example when iterating over them using a
for
loop (to be able to usecontinue
,break
andreturn
, which is not easy to do with.each()
) or when mapping a set of elements to an array. A jquery/cheerio object somewhat resembles an array of elements, so when iterating over it, I need to convert each element to its own jquery/cheerio object in order to be able to use the jquery/cheerio API on it. Here is a real-world example with jquery:The conversion from the element to the jquery object happens by doing
const $p = $(p);
. In cheerio, this works the same, but$
is the root object of the document, rather than a global variable. This means that in a reusable function as the one above, I don't automatically have access to it. I can see 4 options how to do this. I'm not really happy with any of these, so I'm wondering if there is a better/recommended way how to do this.1. Pass the root object to the function:
In this case I'm basically passing the
$
root object around as an additional parameter. I find it quite cumbersome to declare and pass the additional parameter on every function.2. Use a custom helper function to convert a cheerio object to an array or iterator:
This is the most elegant way that I could think of. The only disadvantage that I can think of is the additional code that is needed.
3. Use the internal
._make()
function (or._root()
property):This seems to be the way how cheerio constructs these objects internally. It's not very elegant, as the method is not public and does not exist in the TypeScript typings.
4. Construct the cheerio object with a different root:
This resembles the jquery approach, but I suspect that it might have some unwanted side effects because the parser options are not passed along.
Beta Was this translation helpful? Give feedback.
All reactions