Retrieving a property of a JSON object by index?

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

    You could iterate over the object and assign properties to indexes, like this:

    var lookup = [];
    var i = 0;
    
    for (var name in obj) {
        if (obj.hasOwnProperty(name)) {
            lookup[i] = obj[name];
            i++;
        }
    }
    
    lookup[2] ...
    

    However, as the others have said, the keys are in principle unordered. If you have code which depends on the corder, consider it a hack. Make sure you have unit tests so that you will know when it breaks.

    0 讨论(0)
  • 2020-12-02 13:34

    Here you can access "set2" property following:

        var obj = {
            "set1": [1, 2, 3],
            "set2": [4, 5, 6, 7, 8],
            "set3": [9, 10, 11, 12]
        };
    
        var output = Object.keys(obj)[1];
    

    Object.keys return all the keys of provided object as Array..

    0 讨论(0)
  • 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));
    
    0 讨论(0)
  • 2020-12-02 13:39

    Simple solution, just one line..

    var obj = {
        "set1": [1, 2, 3],
        "set2": [4, 5, 6, 7, 8],
        "set3": [9, 10, 11, 12]
    };
    
    obj = Object.values(obj);
    
    obj[1]....
    
    0 讨论(0)
  • 2020-12-02 13:41

    There is no "second property" -- when you say var obj = { ... }, the properties inside the braces are unordered. Even a 'for' loop walking through them might return them in different orders on different JavaScript implementations.

    0 讨论(0)
  • 2020-12-02 13:46

    Objects in JavaScript are collections of unordered properties. Objects are hashtables.

    If you want your properties to be in alphabetical order, one possible solution would be to create an index for your properties in a separate array. Just a few hours ago, I answered a question on Stack Overflow which you may want to check out:

    • Iterating over a JavaScript object in sort order based on particular key value of a child object

    Here's a quick adaptation for your object1:

    var obj = {
        "set1": [1, 2, 3],
        "set2": [4, 5, 6, 7, 8],
        "set3": [9, 10, 11, 12]
    };
    
    var index = [];
    
    // build the index
    for (var x in obj) {
       index.push(x);
    }
    
    // sort the index
    index.sort(function (a, b) {    
       return a == b ? 0 : (a > b ? 1 : -1); 
    }); 
    

    Then you would be able to do the following:

    console.log(obj[index[1]]);
    

    The answer I cited earlier proposes a reusable solution to iterate over such an object. That is unless you can change your JSON to as @Jacob Relkin suggested in the other answer, which could be easier.


    1 You may want to use the hasOwnProperty() method to ensure that the properties belong to your object and are not inherited from Object.prototype.

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