繁体   English   中英

打字稿:Map.has 没有返回正确的值

[英]Typescript: Map.has doesn't return correct value

我正在遍历打字稿数组并将对象添加到地图。 键应该是唯一的,所以我检查它们是否已经存在于 Map.has

但是,由于某种原因,它没有返回正确的值并添加了重复的键。 请检查我的代码:

    for (const userAccount of res) {

        // create key if doesn't exist
        if (!this.sourceSystemsUserAccounts.has(userAccount.sourceSystem)) {
            this.sourceSystemsUserAccounts.set(userAccount.sourceSystem, new Array<UserAccount>());
        }

        // add user account to map and set new array
        const value: Array<UserAccount> = this.sourceSystemsUserAccounts.get(userAccount.sourceSystem);
        value.push(userAccount);
        this.sourceSystemsUserAccounts.set(userAccount.sourceSystem, value);
    }

它们的键(源系统对象)如下所示:

export class SourceSystems implements BaseEntity {
    constructor(
        public id?: number,
        public name?: string,
        public identifier?: string,
        public currencies?: Array<Currency>,
        public lendingActivated?: boolean,
        public walletApiActivated?: boolean,
    ) {
        this.lendingActivated = false;
    }
}

第三次迭代后,我的地图如下所示: Chrome 调试器中的映射

有谁知道我做错了什么?

更新:在这里您可以看到完整响应对象的预览: 响应对象

你有一个类作为关键又名。 class SourceSystems 类的两个实例不相等 因此重复。

一个解决方案

对地图使用字符串键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM