javascript - 为什么我的 TypeScript 伪字典似乎只存储关键字符串?

标签 javascript asp.net-mvc typescript

本着 Ryan Cavanaugh 接受的答案(我不允许链接到该答案)的精神,该答案提出了以下结构:

var map: { [email: string]: Customer; } = { };

为了模拟 Customer 的字典,其中字符串键是电子邮件地址,我尝试实现一个类似的 SelectionOtherInputDescriptor1 伪字典:

export class SelectionOtherInputDescriptor {
    constructor(public selectionName: string, public otherKey: any, public otherInputElementId: string) { }
}
export class SelectionOtherInputHelper {
    selectionsWithOther: { [selectionKey: string]: SelectionOtherInputDescriptor; } = {};
    getAllSelectionOthers() {
        var things = $("[" + ATT_SELECTION_OTHER_FOR + "]");           
        for (var i = 0; i < things.length; i++) {         
            var selectionName = $(things[i]).attr(ATT_SELECTION_OTHER_FOR);
            var desc = new SelectionOtherInputDescriptor(selectionName, 0, $(things[i]).attr("id"));
            this.selectionsWithOther[selectionName] = desc;
        };
        for (var i in this.selectionsWithOther) {
            window.console.log(i);
        }
    };
}

但是,我的变量 selectionsWithOther 似乎始终只包含三个字符串(在具有三个选择和其他元素的测试页面中),即 selectionKey 值。

1 描述当用户在选择元素上选择“其他”时捕获实际值的输入。

最佳答案

这只是跟踪索引字符串(您的selectionKey):

for (var i in this.selectionsWithOther) {
    window.console.log(i);
}

要跟踪映射到该索引的值,您需要:

for (var i in this.selectionsWithOther) {
    window.console.log(this.selectionsWithOther[i]); // Will trace 'Object'
    window.console.log(this.selectionsWithOther[i].selectionName); // Should trace the value of the property on that object
}

有关 JS 中关联数组的更多信息:http://www.quirksmode.org/js/associative.html

关于javascript - 为什么我的 TypeScript 伪字典似乎只存储关键字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15223715/

相关文章:

javascript - 用输入字段 Javascript/Angular2 替换某些单词

javascript - 如何添加去抖动以响应 onChange 输入

javascript - 根据 Angular 4 中的某些条件,在子级中单击按钮时,从父级中的数组中删除特定的组件实例

javascript - parseFloat ('number' ).toFixed(2) 发生了什么?

javascript - 无法在anglar4中导入RX

asp.net-mvc - 用于 session 超时处理的 session 启动和操作过滤器

asp.net-mvc - MVC4 RouteConfig.cs 不返回静态文件 robots.txt

c# - MVC5 SQL 左连接查询

javascript - 如何编辑选择表单箭头? ( Bootstrap 4)

javascript - 多对象 knockout 映射