0

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

3
  • 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? Commented Dec 15, 2009 at 19:13
  • What kind of error(s) are you getting? Commented Dec 15, 2009 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. Commented Dec 15, 2009 at 19:50

3 Answers 3

12

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

Comments

3

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.

1 Comment

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

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

1 Comment

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.

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.