Link to home
Start Free TrialLog in
Avatar of eugene007
eugene007

asked on

angular.forEach access outside variable

I intend to access the outer variable stated below

if(key==='data'){
	outer;  //outer variable
}

Open in new window


Inside the following like of code

angular.forEach(outer.permission, function(outer_perm, key) {	
        //I want to access outer
 });

Open in new window


here is the full line of code:

			angular.forEach($array, function(outer, key) {			
				if(key==='data'){
				    outer;
				}
				if(key==='try'){
					angular.forEach(outer, function(outer, key){
						 $module_name = outer.name; 
						 //console.log($module_name);
						 angular.forEach(outer.permission, function(outer_perm, key) {	
								//I want to access outer
						 });
					});
				}
			});

Open in new window


Your help is kindly appreciated.

Thank You.
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eugene007
eugene007

ASKER

if(key==='data'){
     console.log(outer);
}

Open in new window


Actual fact is i would like to store this outer array into a variable and use it inside the loop.

for instance

if(key==='data'){
	var test = outer;
}

Open in new window


			angular.forEach($array, function(outer, key) {			
				if(key==='data'){
				     var test = outer;
				}
				if(key==='try'){
					angular.forEach(outer, function(outerItem, key){
						 $module_name = outerItem.name; 
						 //console.log($module_name);
						 angular.forEach(outerItem.permission, function(outer_perm, key) {	
						         console.log(test); 
						 });
					});
				}
			});

Open in new window


However I keep getting undefined message.
Your code relies on the "data" key to be processed before the "try" key in the object, but the order of the items when you iterate them is undefined, and the order actually differs between browsers.

If you know that the items exists, you could just pick up the value from the object before the loop:

var test = $array.data;

Open in new window