How to create dictionary and add key–value pairs dynamically?

后端 未结 15 772
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 00:37

From post:

Sending a JSON array to be received as a Dictionary

I’m trying to do this same thing as that post. The only issue is that I d

15条回答
  •  长情又很酷
    2020-11-28 01:25

    You could create a class Dictionary so you can interact with the Dictionary list easily:

    class Dictionary {
      constructor() {
        this.items = {};
      }
      has(key) {
        return key in this.items;
      }
      set(key,value) {
        this.items[key] = value;
      }
      delete(key) {
        if( this.has(key) ){
          delete this.items[key]
          return true;
        }
        return false;
      }
    }
    
    var d = new Dictionary();
    d.set(1, "value1")
    d.set(2, "value2")
    d.set(3, "value3")
    console.log(d.has(2));
    d.delete(2);
    console.log(d.has(2));

提交回复
热议问题