0

I'm having a bit of a problem so i have an array with objects when i log it it displays everything fine but when i try the .length function it returns 0 for some reason.

Here's my code:

async getTicketType(updatedTicketType) {
    await this.getTicketTypes();
    if (this.typeOptions) {
        console.log('ik kom hier');
        console.log(this.typeOptions);
        console.log(this.typeOptions.length);
        for (let i = 0; i < this.typeOptions.length; i++) {
            console.log('ik kom hier');
            if (this.typeOptions[i]["value"] === updatedTicketType) {
                this.currentTypeOptions = this.typeOptions[i];
            }
        }
    }
}

And here's a picture of the logs:

And here's a picture of the logs

Tân
  • 1
  • 15
  • 56
  • 102

2 Answers2

0

This is because your array has 10 undefined values therefore it does not actually have a length property, if you make sure that there is some value in each element then you will be able to get the array. It's a feature of JS and how the length property on the array is computed.

Michael
  • 4,538
  • 5
  • 31
  • 58
0

You can try to parse it to an array again:

// arr is an array with contains 10 items
var arr = JSON.parse(JSON.stringify(this.typeOptions));

Then, continuing with the loop...

Tân
  • 1
  • 15
  • 56
  • 102