如何在javascript中获取foreach循环之外的数据?

时间:2015-08-19 03:28:28

标签: javascript arrays ajax json

我希望在我的foreach之外获得我的uid的价值。我该怎么做。 。请帮忙

这是我的代码



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);
          }
        });

      }




这是我的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"}]

4 个答案:

答案 0 :(得分:2)

注意:首先,您必须添加全局数组变量。 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]);
}
          }
        });
  }

答案 1 :(得分:1)

您可以声明全局变量

    var newVariable = [];

    getParticipant(conf_url, function(data) {
        data.forEach(function(obj){
           newVariable.push(obj['uid']);
        });
    });

newVariable现在可以在foreach之外访问,但检查您的作业,您正在使用循环,因此预计您有多个值。

答案 2 :(得分:0)

你尝试过使用它的功能吗?

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);
          }
        });

      }

答案 3 :(得分:0)

这会将所有uid转换为数组:

getParticipant(conf_url, function(data) {
    data.map(function(obj){
       return obj.uid;
    });
});

map根据另一个创建一个新数组。该函数返回您要查找的内容(obj.uid)。