2

I am confused by how to access an element by number that matches a named key. My data looks like this,

item_a
    lot_1
       alpha : value
       beta : value
       charlie : value
    lot_2
       alpha : value
       beta : value
       charlie : value

and I am able to extract the set of data for item_a and I get two objects in v as expected using

var getMyItems="item_a";
var myJson = $.getJSON( "/jsonData.html", function(data) {
    $.each(data[getMyItems], function (k, v) {

Now I would like to access just one of the sub array lot_1 || lot_2 by array element number only, ie [0] || [1].

I have tried combinations of data[getMyItems][0] but do not get the desired result of returning,

 var getMyItems="item_a";
    var myJson = $.getJSON( "/jsonData.html", function(data) {
        $.each(data[getMyItems][0], function (k, v) {


var getMyItems="item_a";
var myJson = $.getJSON( "/jsonData.html", function(data) {
    $.each(data[getMyItems], function (k, v) {
        $.each(v[0], function (key, val) {
art vanderlay
  • 1,993
  • 3
  • 27
  • 51
  • 2
    This is not a good idea. the order of properties in an object are not guaranteed: http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order - it's likely that [1] will NOT be what you expect it to be. – random_user_name Aug 23 '16 at 01:30
  • If you need order, use an array, not a plain object. By the way, if you have JSON data it would help if you presented it to us in actual JSON format so that we can see clearly what is an array and what is a plain object - as currently shown it's not clear if `item_a` is an object with two properties, or an array with two elements each of which is an object with one property. – nnnnnn Aug 23 '16 at 01:51

2 Answers2

2

To get 0, you can do:

var getMyItems="item_a";
var obj = data[getMyItems][0];
var myJson obj[Object.keys(obj)[0]];
Arun Ghosh
  • 6,820
  • 1
  • 22
  • 37
  • marking this as correct as it does as asked, but on reflection and digging deeper the risk to un-ordered objects (yup 2016 and JS still can't keep it story straight) is still a valid concern – art vanderlay Aug 23 '16 at 02:35
  • **For the record** - this method is not a good idea. The order of properties are not guaranteed in a javascript object. `lot_2` could very well be before `lot_1`, which would be unexpected and incorrect data. – random_user_name Aug 23 '16 at 12:40
0

Can't really do that the way you want, but here is a workaround - define a new function:

data[getMyItems].get = function(n) {
    return this[Object.keys(this)[n]];
}

Then use it as follows to access an object property by index:

var x = data[getMyItems].get(0)
  • 1
    Not a good idea. The order of properties are not guaranteed in an object. – random_user_name Aug 23 '16 at 01:31
  • @cale_b, does that include JSON data as that would seem fixed to me, or are you saying that `lot_2` could actually come before `lot_1` in an indexing order and is not guaranteed? – art vanderlay Aug 23 '16 at 01:34
  • @artvanderlay - JSON data is a *string* - what you are manipulating in your code is the object that was created by parsing the JSON (your code doesn't explictly parse it, but jQuery does that for you). In practice, although not guarnateed, the order will *probably* be the same in most browsers, but I for one don't want to deploy code that will *probably* work... – nnnnnn Aug 23 '16 at 01:54
  • Yeah there were some problems with the order in IE after you add/delete some items, but for the quick and rough non-production code this should do. Otherwise obviously just use for (... in ...) –  Aug 23 '16 at 02:09