我正在使用带有Cheerio结果列表的map
返回属性值。我想要的是一个包含属性值列表(在本例中为ID)的变量,但是我却在获取ID和额外的数据。
以下代码显示ID的列表:
let ids = $('[data-profileid]').map(function() {
console.log($(this).attr('data-profileid'))
})
结果:
1012938412
493240324
123948532
423948234
...
但是,以下代码以不同的格式返回ID:
let ids = $('[data-profileid]').map(function() {
return $(this).attr('data-profileid')
})
console.log(ids)
结果:
...
'69': '234234234,
'70': '9328402397432',
'71': '1324235234',
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
_root:
{ '0':
{ type: 'root',
name: 'root',
attribs: {},
...
这些额外的数据是什么?当然不是必需的。我宁愿只有一个普通的数组。
最佳答案
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.
所以看起来这应该工作:
let ids = $('[data-profileid]').map(function() {
return $(this).attr('data-profileid')
}).get()
关于node.js - Cheerio map 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044690/