0

I would get a length of an array that I pass in the parameter of a function

I have an array of object

groupList:group[]=[];

in selectItem I call testExistinginArray function

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
        }
    }
}

Actually I get undefined length error

TypeError: Cannot read property 'length' of undefined
5
  • How is selectItem called ? Commented Mar 21, 2018 at 11:20
  • in html input ` (onSelect)="selectItem($event)"` of this component ` Commented Mar 21, 2018 at 11:23
  • 6
    Use type.length instead of this.type.length. Here type is not function variable, it's argument variable. So you can't read using this Commented Mar 21, 2018 at 11:23
  • yes I have forgotten to delete this after changing var. you can put it as an answer I'll accept it Commented Mar 21, 2018 at 11:27
  • @infodev Added as answer. Commented Mar 21, 2018 at 11:56

2 Answers 2

1

Use type.length instead of this.type.length. Here type is not function variable, it's argument variable. So you can't read using this

Sign up to request clarification or add additional context in comments.

Comments

0

You tried to extract attribute length from this.type, not from function parameter type. Looks like typo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.