我正在使用cheerio运行 Node 。我有这个函数,需要显示数组中包含的元素的属性值。
for (var row = 0; row < array.length; row++) {
console.log("Row Length: " + array[row].length);
for (var col = 0; col < array[row].length; col++) {
console.log("Inside:");
console.log(array[row][col].getAttribute('val'));
}
}
我的数组包含使用 push
填充的列表项
array.push($('[title!="CATA"][collection="items"]'));
我得到了“行长度”所需的输出,它显示“内部”,但随后显示“类型错误:未定义不是函数”。我也尝试过使用 .attr('val') 但出现同样的错误。
执行 console.log(array[row][col]) 给出以下结果。
{ type: 'tag',
name: 'li',
attribs:
{ id: 'Amber',
collection: 'items',
title: 'disk',
val: '10|9|2|10|7|13|2|10|12|2|2|5|3|3|5|2|8|7|5|10|4|6|6|3|4|6|2|8|11|2|10|7|9|9|10|8' },
children:
[ { data: 'disk : 10|9|2|10|7|13|2|10|12|2|2|5|3|3|5|2|8|7|5|10|4|6|6|3|4|6|2|8|11|2|10|7|9|9|10|8',
type: 'text',
next: null,
prev: null,
parent: [Circular] } ],
next:
{ type: 'tag',
name: 'li',
attribs:
{ id: 'Amber',
collection: 'items',
title: 'usb',
val: '3|3|2|4|2|4|3|3|3|13|5|13|2|13|2|5|5|3|4|3|6|3|2|2|5|13|2|3|2|13|2|13|2|2|13|4' },
children: [ [Object] ],
next:
.
.
.
我想做的就是访问“val”。
最佳答案
我认为错误是由这一行产生的
console.log(array[row][col].getAttribute('val'));
您正在非对象上调用 .getAttribute。给出完整的列表。
关于javascript - 在 NodeJS 中使用数组时“未定义不是函数”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986575/