1

I want to be able to access the value of a nested each() function outside of the function.

My approach was to:

  • defined the variable outside of the function,
  • update it within the function,
  • be able to access the updated value outside of the function.

But that doesn't work:

jsFiddle

http://jsfiddle.net/rwone/ESXB4/

jQuery

var my_array = [{"check": "tom", "nested_array": [{"nested_array_2": ["1", "2"], "name": "tom", "image": "one.jpg"}, {"nested_array_2": ["1", "2"], "name": "bob", "image": "two.jpg"}]}];

var check = my_array[0].check;

var desired_value = "hello";

$.each(my_array, function (obj, v) {
    $.each(v.nested_array, function (i, value) {
        if (value.name == check) {
            var desired_value = value.image;
            alert(desired_value);  // this returns `one.jpg`, the variable has been updated.  
        }
    });
});

//alert(desired_value); // this alerts 'hello',  the variable has not been updated.  

If the above is the wrong approach, could anyone demonstrate how to access the returned value of a nested each() loop?

Edit: I should probably say, I was cautious to use a global var at any stage, as this can cause problems later on in the application.

Edit 2: I thought using a global var within the function would forever and always define the variable (unless it was globally defined again), but it doesn't seem to eg:

var desired_value = "hello";

$.each(my_array, function (obj, v) {
$.each(v.nested_array, function (i, value) {
if (value.name == check) {
desired_value = value.image; // 'global'
alert(desired_value);  // this returns `one.jpg`, the variable has been updated.  
}
});
});

alert(desired_value); // this alerts 'one.jpg',  the variable has been updated.  

var desired_value = "new value";

alert(desired_value); // this alerts 'new value'

Updated jsFiddle

http://jsfiddle.net/rwone/ESXB4/2/

1
  • This is very basic javascript. Remove var from your inner-most desired_value, so it becomes: desired_value = value.image;. Commented Jan 27, 2014 at 10:27

2 Answers 2

1

You are shadowing desired_value from the outer scope with var desired_value = value.image;. This creates a local variable named desired_value within the loop function. You should remove var here.

Sign up to request clarification or add additional context in comments.

4 Comments

This was first answer so I should accept it. Could you provide further information as to why 'globally' assigning the variable within the function does not override later assignment with var - see updated post and fiddle.
Ah excellent thanks, this was very informative "If you're in a function then "var" will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it)" (source)
4 years later and this was perfect! Thank you @AlexanderTokarev
1

Updated FIDDLE

        var my_array = [{"check": "tom", "nested_array": [{"nested_array_2": ["1", "2"], "name": "tom", "image": "one.jpg"}, {"nested_array_2": ["1", "2"], "name": "bob", "image": "two.jpg"}]}];

var check = my_array[0].check;

var desired_value = "hello";

$.each(my_array, function (obj, v) {
$.each(v.nested_array, function (i, value) {
if (value.name == check) {
desired_value = value.image;  // here changes was made
alert(desired_value);  
}
});
});

alert(desired_value); 

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.