How to access the variable fetched inside .each() method and use it outside in the script in JQuery?

Clash Royale CLAN TAG#URR8PPP
How to access the variable fetched inside .each() method and use it outside in the script in JQuery?
I am getting some result using .each() function but I can only access those result/variable inside the .each() method. In case I want to use the variable from outside the method in anywhere in the script, how to do that.
.each()
.each()
Below is my code sample:
var pass_no=;
var pass_status=;
var pass_cur_status=;
var pass_no_j='';
var pass_status_j='';
var pass_cur_status_j='';
$.each(result.passengers, function(idx, da)
//status is available as da.status
$('#disp_pnr').css('display','none');
$('#pnr_entries').css("display","none");
$('#disp_tbl').css("display","");
pass_no.push(da.no);
pass_status.push(da.booking_status);
pass_cur_status.push(da.current_status);
//$('#disp_tbl').append("<tr><td>Passenger "+da.no+"</td><td>"+da.booking_status+"</td><td>"+da.current_status+"</td></table>");
);
pass_no_j = JSON.stringify(pass_no);
pass_status_j = JSON.stringify(pass_status);
pass_cur_status_j = JSON.stringify(pass_cur_status);
console.log("dfdsf"+pass_no_j);
Here, I am getting passenger no. in variable da.no and da.booking_status in da.current_status. How must I use it outside the .each method.
da.no
da.booking_status
da.current_status
.each
1 Answer
1
Try out by using a global variable concept:
var global_idx = ''; // Top of JS
$.each(result, function(idx, da)
global_idx = idx;
console.log(global_idx);
);
console.log(global_idx); // Anywhere in JS
This is in a each() so last data will be getting in global variable. If you need all, you have to set in array concept.
each()
var global_Array = new Array();
$.each(result, function(idx, da)
global_Array.push(idx); // storing in array
);
console.log(JSON.stringify(global_Array)); // Anywhere in JS
Working. Thanks
– Md Kamran Azam
Aug 8 at 10:03
what if there is more than one value for the result. how to store in this case and how to iterate.
– Md Kamran Azam
Aug 8 at 10:22
you van set an array and using push() to store it in array. I have updated answer
– Sinto
Aug 8 at 10:26
Great. Working. Thanks
– Md Kamran Azam
Aug 8 at 10:32
If you done just like
global_Array.push(idx); the out will be as like as your expectation– Sinto
Aug 8 at 11:57
global_Array.push(idx);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try out by setting a global variable.
– Sinto
Aug 8 at 9:52