Retrieving a property of a JSON object by index?

后端 未结 11 965
情书的邮戳
情书的邮戳 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:58

    Jeroen Vervaeke's answer is modular and the works fine, but it can cause problems if it is using with jQuery or other libraries that count on "object-as-hashtables" feature of Javascript.

    I modified it a little to make usable with these libs.

    function getByIndex(obj, index) {
      return obj[Object.keys(obj)[index]];
    }
    

提交回复
热议问题