0

本着 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描述当用户在选择元素上选择“其他”时捕获实际值的输入。

4

1 回答 1

3

这只是跟踪索引字符串(你的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

于 2013-03-05T14:11:59.423 回答