JavaScript dictionary with names

前端 未结 8 1179
孤街浪徒
孤街浪徒 2020-12-12 18:08

I want to create a dictionary in JavaScript like the following:

myMappings = [
    { "Name": 10%},
    { "Phone": 10%},
    { "Addres         


        
8条回答
  •  庸人自扰
    2020-12-12 18:31

    An easier, native and more efficient way of emulating a dict in JavaScript than a hash table:

    It also exploits that JavaScript is weakly typed. Rather type inference.

    Here's how (an excerpt from Google Chrome's console):

    var myDict = {};
    
    myDict.one = 1;
    1
    
    myDict.two = 2;
    2
    
    if (myDict.hasOwnProperty('three'))
    {
      console.log(myDict.two);
    }
    else
    {
      console.log('Key does not exist!');
    }
    Key does not exist! VM361:8
    
    if (myDict.hasOwnProperty('two'))
    {
      console.log(myDict.two);
    }
    else
    {
      console.log('Key does not exist!');
    }
    2 VM362:4
    
    Object.keys(myDict);
    ["one", "two"]
    
    delete(myDict.two);
    true
    
    myDict.hasOwnProperty('two');
    false
    
    myDict.two
    undefined
    
    myDict.one
    1
    

提交回复
热议问题