我会得到在函数参数中传递的数组的长度
我有一个对象数组
groupList:group[]=[];
在selectItem
中我调用testExistinginArray
函数
selectItem (event) {
if(!this.toggle) {
this.groupList.push(event);
}
else{
this.ejList.push(event);
if(!this.testExistinginArray(event,this.groupList)) { //I pass groupList as parameter
this.groupList.push({code:event.codeBusinessGroup,name:event.nameBusinessGroup})
}
}
}
testExistinginArray(event,type: any[]) {
for(let i=0;i<this.type.length;i++) {
if (this.type[i].code.indexOf(event.codeBusinessGroup)===-1) {
return false
}
else{
return true
}
}
}
实际上我得到了未定义的长度错误
TypeError: Cannot read property 'length' of undefined
最佳答案
使用type.length
而不是this.type.length
。这里的type
不是函数变量,而是参数变量。所以你无法使用 this
关于javascript - 无法获取作为函数参数传递的数组长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404944/