1

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 (i.e. "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]

2
  • You're missing a comma between the array values in the data array. Commented Sep 17, 2008 at 19:50
  • thanks a lot for the help..yes, I changed the names for the post and messed up the syntax, thanks again Commented Sep 17, 2008 at 20:40

9 Answers 9

4

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]] );
}           
Sign up to request clarification or add additional context in comments.

Comments

2

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 (i.e., 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.

1 Comment

[{},{},{},] <-- last comma here not needed.
1

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...

Comments

1
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 :)

Comments

1

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"

Comments

0

perhaps try data[0].Make

Comments

0

Close, you'd use

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

Comments

0

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"]

1 Comment

thanks for the response, you are right about the commas, I changed the names for the post and messed up the syntax
0

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>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.