7

I have a JSON as follows

{
  columns      : [RULE_ID,COUNTRY_CODE],
  RULE_ID      : [1,2,3,7,9,101,102,103,104,105,106,4,5,100,30], 
  COUNTRY_CODE : [US,US,CA,US,FR,GB,GB,UM,AF,AF,AL,CA,US,US,US]
}

I need to retrive the column names from the columns entry and then use it to search the rest of the entries using jquery. For example I get each column using

jQuery.each(data.columns, function(i,column))

I need to loop throgh the rest of the entries using the values I get from the previous loop. ie without hardcoding COUNTRY_CODE or RULE_ID. What is the best way to do that using Jquery?

1
  • nitpicking: this is a JavaScript object, but it's not JSON (the string-based data interchange format) Commented Dec 9, 2008 at 12:25

2 Answers 2

9
jQuery.each(data.columns, function(i,column) {
    jQuery.each(data[column], function(i, row) {
    ....
    });
});
Sign up to request clarification or add additional context in comments.

Comments

4

Some comments:

Paolo is right, you can use data[column] to get the list of values:

jQuery.each(data.columns, function(iCol,column) {
    jQuery.each(data[column], function(iRow, row) {
    ....
    });
});

Do you need the columns information ? You could get the list of "columns" by iterating directly over data:

for( var column in data )
{
    jQuery.each(data[column], function(i, row) {
    ....
    });
}

Not really a question but your JSON is not valid JSON, I hope this was only an example and not your real data:

{
  "columns"      : ["RULE_ID","COUNTRY_CODE"],
  "RULE_ID"      : [1,2,3,7,9,101,102,103,104,105,106,4,5,100,30], 
  "COUNTRY_CODE" : ["US","US","CA","US","FR","GB","GB","UM","AF","AF","AL","CA","US","US","US"]
}

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.