Is there a more efficient method for storing and retrieving data besides relying on Array index-based calls?
For instance:
export interface EntityInterface {
id: number;
name: string;
age: number;
}
export class ClassName {
entities: EntityInterface[] = [];
temporaryIndex: number = 0;
createEntity(name: string, age: number) {
const temporaryId = ++this.temporaryIndex;
this.entities.push({
id: temporaryId,
name: name,
age: age
});
}
getEntityById(id: number): number | undefined {
const entityIndex = this.entities.findIndex((entity) => entity.id === id);
if (entityIndex === -1) {
return;
}
return entityIndex;
}
getEntityName(id: number): string | undefined {
const index = this.getEntityById(id);
if (! index) {
return;
}
return this.entities[index].name;
}
}
It's important to note that utilizing an external database isn't essential in this scenario since data loss upon application shutdown is not a concern.