JS associative object with duplicate names

前端 未结 8 2066
伪装坚强ぢ
伪装坚强ぢ 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:54

    That is not an array that is an object. You'd be better creating a property of the object that is an array and store the different values in there.

    var myarray = {
       "field_1": "lorem ipsum",
       "field_array": []
    };
    
    myarray.field_array.push(value);
    

    then just loop through that property of the array.

提交回复
热议问题