我正在try 理解是否有比使用基于数组索引的调用更好的存储和访问数据的方法.

例如:

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;
    }
}

请记住,在这种情况下不需要使用外部数据库,因为我不在乎应用程序关闭后是否会丢失数据.

推荐答案

如果您想在类型脚本中(因此在JavaScript中)通过一个数字键查找一个值,那么您可能应该使用一个用于按键查找的数据 struct .

平面JavaScript objects会自动执行此操作;在TypeScrip中,您可以为对象指定数字index signature:

class ClassName {
    entities: { [k: number]: EntityInterface | undefined } = {};

    temporaryIndex: number = 0;

    createEntity(name: string, age: number) {
        const temporaryId = ++this.temporaryIndex;
        this.entities[temporaryId] = {
            id: temporaryId,
            name: name,
            age: age
        };
    }

    // this is unlikely to be useful
    getEntityById(id: number): number | undefined {
        return this.entities[id] ? id : undefined
    }

    getEntityName(id: number): string | undefined {
        return this.entities[id]?.name;
    }
}

或者你也可以使用一个JAVASCRIPT Map,你可以看出它在number个关键点上保存着EntityInterface个值:

class ClassName {
    entities = new Map<number, EntityInterface>()

    temporaryIndex: number = 0;

    createEntity(name: string, age: number) {
        const temporaryId = ++this.temporaryIndex;
        this.entities.set(temporaryId, {
            id: temporaryId,
            name: name,
            age: age
        });
    }

    // this is unlikely to be useful
    getEntityById(id: number): number | undefined {
        return this.entities.has(id) ? id : undefined
    }

    getEntityName(id: number): string | undefined {
        return this.entities.get(id)?.name;
    }
}

这两种方法的行为应该非常相似,特别是与迭代数组相比.有关在它们之间进行 Select 的帮助,请参阅Map vs Object in JavaScriptthe MDN documentation on Map vs Object.

在这两个版本中,getEntityById(id)现在要么返回它的输入,要么返回undefined,所以它不太可能有用.您只需立即查找this.entities,并将id作为关键字,然后处理可能的undefined结果.

Playground link to code

Typescript相关问答推荐

如何根据参数的值缩小函数内的签名范围?

根据另一个成员推断类成员的类型

如何将类型从变量参数转换为返回中的不同形状?

如何缩小变量访问的对象属性范围?

如何提取密钥及其对应的属性类型,以供在新类型中使用?

如何按不能保证的功能过滤按键?

我想创建一个只需要一个未定义属性的打字脚本类型

我可以为情态车使用棱角形的路由守卫吗?

使用打字Angular 中的通用数据创建状态管理

函数重载中的不正确TS建议

为什么Typescript不能推断类型?

TypeScrip:强制使用动态密钥

在Cypress中,是否可以在不标识错误的情况下对元素调用.Click()方法?

带投掷的收窄类型

如何在Reaction 18、Reaction-Rout6中的导航栏中获取路由参数

替换typescript错误;X类型的表达式可以';t用于索引类型Y;带有未定义

映射类型不是数组类型

在tabel管理区域react js上设置特定顺序

如何提取具有索引签名的类型中定义的键

将 Angular 从 14 升级到 16 后出现环境变量注入问题