Retrieving a property of a JSON object by index?

后端 未结 11 972
情书的邮戳
情书的邮戳 2020-12-02 13:28

Assuming this JSON object:

var obj = {
    \"set1\": [1, 2, 3],
    \"set2\": [4, 5, 6, 7, 8],
    \"set3\": [9, 10, 11, 12]
};

The \"set

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 13:38

    My solution:

    Object.prototype.__index=function(index)
                             {var i=-1;
                              for (var key in this)
                                  {if (this.hasOwnProperty(key) && typeof(this[key])!=='function')
                                      {++i;
                                      }
                                   if (i>=index)
                                      {return this[key];
                                      }
                                  }
                              return null;
                             }
    aObj={'jack':3, 'peter':4, '5':'col', 'kk':function(){alert('hell');}, 'till':'ding'};
    alert(aObj.__index(4));
    

提交回复
热议问题