Avoid instantiations inside loops Angular
22:07 28 Jan 2024

I have these lines of code that has bug with cast green software scaning.

   public addFilesToQueue(files: FileList | any): void {

        const addedFiles: AttachmentItem[] = [];

        for (let iFileId = 0; iFileId < files.length; iFileId++) {

            const file = files.item(iFileId);

            const attachment: AttachmentItem = new AttachmentItem(uuid(), file);
            
            if (!attachment || !attachment.file) {
                return;
            }

            this.attachments.push(attachment);
        }

        this.messageBusService
            .addMessage(WebMessageBusChannelNameConstant.attachmentTableListFile, WebMessageBusEventNameConstant.updateAttachmentTableListFile, this.attachments);
       
        for (const availableFile of this.attachments) {
            this.buildUploadRequest(availableFile).pipe(
                switchMap((httpRequest: HttpRequest) => {
                    return this.httpClient
                        .request(httpRequest);
                }),
                map(httpEvent => {
                    return availableFile;
                })
            ).subscribe();
        }
    }

The line that cause the error is: const attachment: AttachmentItem = new AttachmentItem(uuid(), file); Here is the AttachmentItem:

 export declare class AttachmentItem {
    id: string;
    fileName: string;
    fileType: string;
    referenceType: string;
    referenceId: string;
    documentType: string;
    updatedAt: string;
    updatedBy: string;
    file: File;
    status: AttachmentItemUploadStatuses;
    private _progress;
    private _canceller;
    progress: number;
    readonly canceller: Subject;
    constructor(id: string, file: File);
}
angular