Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working with a JSON dataset that has multiple high level objects. If I literally declare the object name, I can loop through the JSON with no trouble, but when I use a variable in its place, I get errors. It appears that it's trying to apply the variable name as the literal name of the object. Here's a quick example

function(data){
    var select = $('#lists select');
    var activeList = $(select+':selected').val();

    $.each(data.activeList, function(i,item){
      // Do stuff    
    });
}

How do I get it to use the value of activeList in this case? To be clear, the variable activeList correctly returns the value of the selected option. If the value of activeList is "Christmas_List", looping through data.activeList should loop through the Christmas_List object within the JSON data. This doesn't work, it appears that data.activeList is looking for the "activeList" object. If I try a literal value in activeList's place (Christmas_List), the loop works properly.

Any ideas? Thanks

share|improve this question
1  
2 problems: where is select getting defined? Also, what is the value returned from val() that you think you can iterate over with .each? –  Roatin Marth Dec 15 '09 at 19:13
    
What kind of error(s) are you getting? –  Justin Swartsel Dec 15 '09 at 19:13
    
select is just a variable pointing to a select dropdown. Sorry, I didn't clarify that. activeList successfully returns the selected value of the dropdown. I've added the relevant code to the question. I'm getting the error "G is undefined" pointing to the jquery library. That is the error I get when I try to loop through an object that doesn't exist in the JSON data. –  SDG Dec 15 '09 at 19:50

3 Answers 3

up vote 7 down vote accepted

Do you mean you have a situation something like this?

$(function() {
    var test = { Foo : [ 1, 2, 3], Bar : [4, 5, 6] }; // Your JSON object
    var prop = "Foo"; //Your select list value
    $.each(test[prop], function() { alert(this); });
});

In which case you want to access the object by key....

test[prop]

rather than directly

test.prop;
share|improve this answer

I'm not entirely certain what you're trying to do here, however, your example code looks awkward, and so perhaps something like this is what you're looking for as an answer:

activeList = []
$(select+":selected").each( function(item) {
    activeList.append( $(item).val() )
});

This would create a list of the :selected values. If not, please clarify the question.

share|improve this answer
    
I really didn't give enough information. activeList returns the selected value of a select dropdown. The value of activeList will correspond to the JSON object I wish to loop through. –  SDG Dec 15 '09 at 20:01

$().val() returns the first value found if you want it as an array you will have to loop through the selected inputs yourself

share|improve this answer
    
the activeList variable returns the value I want it to -- the value of the selected option. That value corresponds with the name of an object in the JSON data - I want to loop through the corresponding object. The problem is that when I try to loop through data.activeList, it appears to be trying to loop through the non existent activeList object instead of using the value of activeList. –  SDG Dec 15 '09 at 20:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.