I am working on a typescript method that eliminates hashtags from a string and saves them in an array within a model. Each element in the hashtag array is assigned a unique UUID along with the complete string added to the model. However, I am facing an issue where the UUID of the last array element being looped over is replacing the UUIDs of the other elements, making them all the same. How can I ensure that each UUID remains unique during the loop?
Model:
import { UUID } from 'angular2-uuid';
export interface Note {
id: UUID;
projectId?: string;
name?: string;
img?: File;
message: string;
date?: Date;
tags?: Tags[];
}
export interface Tags {
id: UUID;
tag: string;
}
Method:
notes: Note[] = [];
tags: Tags[] = [];
newTags = {} as Tags;
newNote = {} as Note;
addNote() {
this.newNote.id = uuid();
this.newNote.projectId = this.project.id;
const regexp = /\B\#\w\w+\b/g;
const result = this.newNote.message.match(regexp);
if (result != null) {
for (let i = 0; i < result.length; i++) {
this.newTags.tag = result[i];
this.newTags.id = uuid();
console.log(this.newTags);
this.tags[i] = this.newTags;
this.newNote.tags = this.tags;
}
}