如何在 javascript 中的 foreach 循环之外获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32086039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i get data outside a foreach loop in javascript?
提问by ar em
I want to get the value of my uid outside my foreach. How can i do it. .Please help
我想在我的 foreach 之外获取我的 uid 的值。我该怎么做。。请帮忙
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
这是我的 json 数据 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"}]
回答by owais ali
note:first of all you have to add global array variable. javascript contain a .pushfunction it will be add your object one be one
注意:首先你必须添加全局数组变量。javascript 包含一个.push函数,它将添加您的对象一个是一个
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]);
}
}
});
}
回答by Sherlock
You can declare a global variable
您可以声明一个全局变量
var newVariable = [];
getParticipant(conf_url, function(data) {
data.forEach(function(obj){
newVariable.push(obj['uid']);
});
});
newVariableis now accessible outside foreach, but check your assignment, you are using loop so expected that you have multiple values.
newVariable现在可以在 foreach 之外访问,但请检查您的分配,您正在使用循环,因此预计您有多个值。
回答by Sina
Did you try to use a function for it?
您是否尝试为此使用函数?
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']);
myVal(obj['uid']);
});
});
// I WANT TO GET THE VALUE OF UID HERE
function myVal (var uid)
{
console.log(uid);
//do something
}
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);
}
});
}

