JS associative object with duplicate names

前端 未结 8 2003
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 22:51

ok, so I have an object like:

var myobject = {
   \"field_1\": \"lorem ipsum\",
   \"field_2\": 1,
   \"field_2\": 2,
   \"field_2\": 6
};

相关标签:
8条回答
  • 2020-12-10 23:32

    You can't do this. The array key must be unique.

    If you've got Firefox/Firebug installed (or similar in another browser), you can try it by entering this into the Firebug console:

    var myarray = {
       "field_1": "lorem ipsum",
       "field_2": 1,
       "field_2": 2,
       "field_2": 6
    };
    console.dir(myarray);
    

    Firebug will respond with:

    field_1      "lorum ipsum"
    field_2      6
    

    in other words, it works, but each subsequent value specified for field_2 overwrites the previous one; you can only have one value for it at a time.

    The closest you can get to what you want is to make field_2 an array in itself, something like this:

    var myarray = {
       "field_1": "lorem ipsum",
       "field_2": [1,2,6]
    };
    

    If you do console.log now, you'll get this:

    field_1      "lorum ipsum"
    field_2
        0        1
        1        2
        2        6
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-10 23:33

    Associative arrays do not exist in Javascript - what you have created is an Object using the JSON format.

    I suspect that something like this will give you more what you are seeking, though I suggest questioning exactly what it is that you are trying to achieve..

    The following code will allow you to access multiple instances of duplicated 'keys', but is

    var myDataset = [
       { "field_1": "lorem ipsum" },
       { "field_2": 1 },
       { "field_2": 2 },
       { "field_2": 6 }
    ];
    
    
    $.each(myDataset, function(valuePairIndex, value)
    {
        $.each(myDataset[valuePairIndex], function(key, value1)
        {
           var valuePair = myDataset[valuePairIndex];
           console.log(valuePairIndex);
           console.log(key + ' = ' + valuePair[key]);
    
    //       console.log('key = ' + key);
    //       console.log('valuePair[key] = ' + valuePair[key]);
        });
    });
    
    0 讨论(0)
  • 2020-12-10 23:38

    It is not possible.

    The resulting object does only contain 2 elements, the first and second field_2 elements are lost on creation.

    0 讨论(0)
  • 2020-12-10 23:47

    The keys must be unique.

    0 讨论(0)
  • 2020-12-10 23:49

    The only way to get around it would be to either change the fields to unique identifiers, or something like:

    var myarray = {
       "field_1": "lorem ipsum",
       "field_2": [
           {"value_1": 1},
           {"value_2": 2},
           {"value_3": 6}
       ]
    };
    
    0 讨论(0)
  • 2020-12-10 23:52

    You're overwriting the same value several times.

    What you want is probably something like:

    var myarray = {
       "field_1": "lorem ipsum",
       "field_2": [1,2,6]
    };
    

    Which could be written in a manner similar to what you currently have:

    var myarray = {};
    
    myarray.field_1 = [];
    myarray.field_1.push('lorem ipsum');
    myarray.field_2 = [];
    myarray.field_2.push(1);
    myarray.field_2.push(2);
    myarray.field_2.push(6);
    

    Note that I made field_1 an array as well, which - for consistency - I thought you might want.

    0 讨论(0)
提交回复
热议问题