Problem with an array

Can anyone see a problem with this code on line 3 with the this.newFileName[i], without the “[i]”, like this: this.newFileName, it works perfect, with the array it doesn’t work. Can anyone see why? Thanks!

  createFileName2(i) {
    console.log("interim " + i);
    this.newFileName[i] = "fish_tank_photo_" + i + ".jpg";
    console.log("New File Name " + i + ": " + this.newFileName[i]);
    return this.newFileName;

}

You don’t show us how this.newFileName is declared or initialized. Is it actually an array?

Sorry, forgot, this is what I have at the very top of the class:

newFileName: any = null;

You are trying to access an empty array - instead of:

this.newFileName[i] = “fish_tank_photo_” + i + “.jpg”;

push the item into the array:

this.newFileName.push(“fish_tank_photo_” + i + “.jpg”);

then this.newFileName[i] will work

2 Likes

I’m new to all of this stuff, but should it not be

newFileName: Array<any> = [];
1 Like

Array<string> is better than using any, but yes the key is actually assigning an array to it.

1 Like