map returns array instead of Cheerio object
See original GitHub issueIn jQuery, $(selector).map(callback)
will return a jQuery object wrapping return values of callback
. In Cheerio, we instead get back an array, each element of which is a length-1 Cheerio object wrapping a return value of callback
.
Issue Analytics
- State:
- Created 10 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Cheerio Map Strange Behaviour - node.js - Stack Overflow
According to http://api.jquery.com/map/: As the return value is a jQuery object, which contains an array, it's very common to call .get() on ...
Read more >How to use map function in Cheerio - Javascript - Tabnine
function parseHTML (html, selector) { const $ = cheerio.load(html) return $(selector) .map(function () { return ($(this).html() || '').replace(/\s+/g, ...
Read more >Extracting data with Cheerio | Algolia
Learn the Cheerio syntax to extract data in the Algolia Crawler, and discover ready-to-use selectors and extractors.
Read more >Class Cheerio<T> Abstract
Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an...
Read more >How To Use .map() to Iterate Through Array Items in JavaScript
One of the most popular methods is the .map() method. .map() creates an array from calling a specific function on each item in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Convert the collection to a JavaScript array first:
Note that the return value will also be an array, though!
Hi @jugglinmike - I’m in agreement with the two points you make. However, I would like to ask for some advice on how to implement your point 2.
Firstly I’m assuming that
Cheerio
in your example is the result ofcheerio.load(html);
(given thatconst cheerio = require('cheerio')
), which would have been executed at some point before moving into themap
callback.Given that assumption, in order to rewrap the node in a cheerio object doing
var $elem = Cheerio(this);
as you describe, I would need to have theCheerio
var in scope, or alternatively have the originalhtml
var in scope so that I could recreate theCheerio
object with anothercheerio.load(html);
. Unfortunately in my code I have neither of these vars in scope at the time I wish to do this. Thus, I am using this little hack:This seems to work fine. It just feels a little messy that I should need the hack. Can you think of a better way?
One solution might be to have
$
as a member of the class rather than creating it instartHere()
so that I can call upon it withthis.$
, although the issue there is that this class works on multiple html documents (with different values ofhtml
) so if I callthis.$ = cheerio.load(html);
I would overwrite the previous value. That said, generally I do these operations in series and not in parallel, so perhaps it would be fine.