我尝试检查一个名为test[]的数组的值"Dep_",并将它的值添加到另一个数组arrayDep。
它之所以失败,是因为它指出该位置的数组是undefined,但在控制台日志中可以看到充满项的数组。
代码:
jsdom.env({
url: "http://www.camara.leg.br/internet/deputado/Dep_Lista.asp?Legislatura=55&Partido=QQ&SX=QQ&Todos=None&UF=QQ&condic=QQ&forma=lista&nome=&ordem=nome&origem=None",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (errors, window) {
var test = [];
var arrayDep = [];
var value;
var substring = "Dep_Detalhe.asp";
var $ = window.$;
$('a').each(function () {
test.push($(this).attr('href'));
});
console.log(test);
// inside console log:
// 'Dep_Detalhe.asp?id=5830400',
// 'mailto:dep.givaldocarimbao@camara.leg.br',
// 'Dep_Detalhe.asp?id=5830706',
// 'mailto:dep.givaldovieira@camara.leg.br',
// 'Dep_Detalhe.asp?id=5830397',
// 'mailto:dep.glauberbraga@camara.leg.br', ...
for (var i = 0; i < test.length; i++) {
value = test[i];
if (value.substring(0,3) === "Dep_") {
// error occurs here it says:
// TypeError: Cannot read property 'substring' of undefined
arrayDep.push(test[i]);
}
}
}
});发布于 2016-03-16 04:02:37
我的数组有未定义的组件,感谢大家,但最好的技巧是使用节点检查器,它可以很容易地验证我的大数组是否有未定义的组件。
https://stackoverflow.com/questions/36020504
复制相似问题