简体   繁体   中英

Javascript array reference

If I have the following:

{"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
}

And I have a "hdrs" name (ie "Make"), how can I reference the data array instances? seems like data["Make"][0] should work...but unable to get the right reference

EDIT

Sorry for the ambiguity.. I can loop through hdrs to get each hdr name, but I need to use each instance value of hdrs to find all the data elements in data (not sure that is any better of an explanation). and I will have it in a variable t since it is JSON (appreciate the re-tagging) I would like to be able to reference with something like this: t.data[hdrs[i]][j]

I had to alter your code a little:

var x = {"hdrs": ["Make","Model","Year"],
         "data" : [ 
           {"Make":"Honda","Model":"Accord","Year":"2008"},
           {"Make":"Toyota","Model":"Corolla","Year":"2008"},
           {"Make":"Honda","Model":"Pilot","Year":"2008"}]
        };

        alert( x.data[0].Make );

EDIT: in response to your edit

var x = {"hdrs": ["Make","Model","Year"],
         "data" : [ 
           {"Make":"Honda","Model":"Accord","Year":"2008"},
           {"Make":"Toyota","Model":"Corolla","Year":"2008"},
           {"Make":"Honda","Model":"Pilot","Year":"2008"}]
        };
var Header = 0; // Make
for( var i = 0; i <= x.data.length - 1; i++ )
{
    alert( x.data[i][x.hdrs[Header]] );
}           

First, you forgot your trailing commas in your data array items.

Try the following:

var obj_hash = {
    "hdrs": ["Make", "Model", "Year"],
    "data": [
        {"Make": "Honda", "Model": "Accord", "Year": "2008"},
        {"Make": "Toyota", "Model": "Corolla", "Year": "2008"},
        {"Make": "Honda", "Model": "Pilot", "Year": "2008"},
    ]
};

var ref_data = obj_hash.data;

alert(ref_data[0].Make);

@Kent Fredric: note that the last comma is not strictly needed, but allows you to more easily move lines around (ie, if you move or add after the last line, and it didn't have a comma, you'd have to specifically remember to add one). I think it's best to always have trailing commas.

So, like this?

var theMap = /* the stuff you posted */;
var someHdr = "Make";
var whichIndex = 0;
var correspondingData = theMap["data"][whichIndex][someHdr];

That should work, if I'm understanding you correctly...

var x = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

x.data[0].Make == "Honda"
x['data'][0]['Make']  == "Honda"

You have your array/hash lookup backwards :)

I'm not sure I understand your question, but...

Assuming the above JSON is the var obj, you want:

obj.data[0]["Make"] // == "Honda"

If you just want to refer to the field referenced by the first header, it would be something like:

obj.data[0][obj.hdrs[0]] // == "Honda"

也许尝试 data[0].Make

Close, you'd use

var x = data[0].Make;
var z = data[0].Model;
var y = data[0].Year;

Your code as displayed is not syntactically correct; it needs some commas. I got this to work:

$foo = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"},
   {"Make":"Toyota","Model":"Corolla","Year":"2008"},
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

and then I can access data as:

$foo["data"][0]["make"]

With the help of the answers (and after getting the inside and outside loops correct) I got this to work:

var t = eval( "(" + request + ")" ) ;
for (var i = 0; i < t.data.length; i++) {
 myTable +=    "<tr>";
 for (var j = 0; j < t.hdrs.length; j++) {
  myTable += "<td>" ;
   if (t.data[i][t.hdrs[j]] == "") {myTable += "&nbsp;" ; }
    else { myTable += t.data[i][t.hdrs[j]] ; }
  myTable += "</td>";
 }
 myTable +=    "</tr>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM