Cheerio: Bug in Cheerio's Map function

Created on 19 Aug 2016  路  1Comment  路  Source: cheeriojs/cheerio

The following code produces an array of ID's:

$('[data-profileid]').map(function(i, elem) {
    console.log($(elem).attr('data-profileid'))
})

Result:

432432432
234235324
2349235325
....

But the following code doesn't produce an array of ID's, instead it produces a list something entirely different with junk on the end.

let ids = $('[data-profileid]').map(function(i, elem) {
    return $(elem).attr('data-profileid')
})

console.log(ids)

Result:

'68': '324324324234',
'69': '35324543',
'70': '546456456',
'71': '123405325', 
options:
 { withDomLvl1: true,
   normalizeWhitespace: false,
   xmlMode: false,
   decodeEntities: true },
  _root:
  { '0':
      { type: 'root', 

Why isn't it just returning a list of ID's instead of Cheerio objects?

Not a bug

Most helpful comment

This is intentional, as explicitly described by this project's documentation:

Pass each element in the current matched set through a function, producing a new Cheerio object containing the return values.

As for the motivation: the map method is implemented in this way for parity with jQuery. From jQuery's documentation:

As the return value is a jQuery object, which contains an array, it's very common to call .get() on the result to work with a basic array.

>All comments

This is intentional, as explicitly described by this project's documentation:

Pass each element in the current matched set through a function, producing a new Cheerio object containing the return values.

As for the motivation: the map method is implemented in this way for parity with jQuery. From jQuery's documentation:

As the return value is a jQuery object, which contains an array, it's very common to call .get() on the result to work with a basic array.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

trevorfrese picture trevorfrese  路  4Comments

misner picture misner  路  3Comments

tndev picture tndev  路  4Comments

Canop picture Canop  路  3Comments

chenweiyj picture chenweiyj  路  5Comments