Mic*_*ael 0 javascript arrays loops for-loop prototype
为什么我的代码不起作用?
var canyonCows = [
{name: "Bessie", type: "cow", hadCalf: "Burt"},
{name: "Donald", type: "bull", hadCalf: null},
{name: "Esther", type: "calf", hadCalf: null},
{name: "Burt", type: "calf", hadCalf: null},
{name: "Sarah", type: "cow", hadCalf: "Esther"},
{name: "Samson", type: "bull", hadCalf: null},
{name: "Delilah", type: "cow", hadCalf: null}
];
Array.prototype.countCattle = function(type){
var counter = 0;
for(var i=0;i<this.lenght;i++){
if (this[i]["type"]==type){
counter++;
}
}
return counter;
};
console.log(canyonCows.countCattle("cow"));
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到的输出0而不是3.
我究竟做错了什么?