Is there a dictionary implementation in JavaScript?

前端 未结 10 2049
萌比男神i
萌比男神i 2020-12-12 22:04

How can I implement an array with an indexer in JavaScript? Is there something like a dictionary in .Net?

10条回答
  •  既然无缘
    2020-12-12 22:19

    var nDictionary = Object.create(null);
    
    function setDictionary(index, value) {
        nDictionary[index] = value;
    }
    
    function getDictionary(index) {
        return nDictionary[index];
    }
    
    setDictionary(81403, "test 1");
    setDictionary(81404, "test 2");
    setDictionary(81405, "test 3");
    setDictionary(81406, "test 4");
    setDictionary(81407, "test 5");
    
    alert(getDictionary(81403));
    

提交回复
热议问题