How can i get data outside a foreach loop in javascript?
17,458
Solution 1
You can declare a global variable
var newVariable = [];
getParticipant(conf_url, function(data) {
data.forEach(function(obj){
newVariable.push(obj['uid']);
});
});
newVariable
is now accessible outside foreach, but check your assignment, you are using loop so expected that you have multiple values.
Solution 2
note: first of all you have to add global array variable. javascript contain a .push function it will be add your object one be one
var globalArray=[];
function getParticipant(conf_uri, handleData) {
$.ajax({
type: "GET",
url: conf_uri,
dataType: "jsonp",
jsonpCallback: 'callback',
contentType: "application/javascript",
success: function(data) {
for (i = 0; i < data.length; i++) {
globalArray.push( data[i]);
}
}
});
}
Author by
ar em
Updated on June 04, 2022Comments
-
ar em about 2 years
I want to get the value of my uid outside my foreach. How can i do it. .Please help
this is my code
var conf_url = "https://192.168.236.33/confbridge_participants/conference_participants.json?cid=0090000007"; getParticipant(conf_url, function(data) { data.forEach(function(obj){ document.write(obj['uid']) }); }); // i want to get the value of uid here // I WANT TO GET THE VALUE OF UID HERE function getParticipant(conf_uri, handleData) { $.ajax({ type: "GET", url: conf_uri, dataType: "jsonp", jsonpCallback: 'callback', contentType: "application/javascript", success: function(data) { handleData(data); console.log(data); } }); }
this is my json data which is
https://192.168.236.33/confbridge_participants/conference_participants.json?cid=0090000007
[{"id":8,"uid":"0090000163","cid":"0090000007","extension":"202","secret":"Myojyo42f!","leader":true,"simultaneous":false,"confbridge_id":6,"created_at":"2015-08-18 09:22:20","updated_at":"2015-08-18 09:22:20"},{"id":11,"uid":"0090000165","cid":"0090000007","extension":"204","secret":"Myojyo42f!","leader":false,"simultaneous":false,"confbridge_id":6,"created_at":"2015-08-18 09:45:36","updated_at":"2015-08-18 09:45:36"}]
-
Sebastian Simon almost 9 yearsWhich
uid
? You appear to have multiple ones. Do you want to get them all out in an array? You could usemap
instead offorEach
then. -
ar em almost 9 yearsHello @Xufox. I want to get them all out in an array. How will i do it?
-
-
Sebastian Simon almost 9 yearsWell,
data.map(…);
creates an array with theuid
s… do you want to use the Array outside the function withingetParticipant
— is that what you mean? Just use the array outside ofdata.map(…);
and insidegetParticipant(conf_url, function(data) {…});
. I guess, this is an asynchronous function, so you kinda have to do it this way. -
ar em almost 9 yearsyes i want to use the array outside the function within getParticipant
-
DarkMakukudo almost 9 years@arem how did you implement it?
-
ar em almost 9 yearsahm . . I want to get the value outside . . foreach . .
-
DarkMakukudo almost 9 yearsi mean, the global variable one, try also to log your
obj
variable to see its contents, use console.log -
ar em almost 9 yearswhen i access the value outside foreach . . only the last value appear.
-
DarkMakukudo almost 9 years@arem yah because the variable is not an array, you should put it in an array to capture all values, i edited my answer to implement it as an array