最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - How can i get data outside a foreach loop in javascript? - Stack Overflow

programmeradmin2浏览0评论

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

      }

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"}]
Share Improve this question asked Aug 19, 2015 at 3:28 ar emar em 4442 gold badges6 silver badges18 bronze badges 2
  • 1 Which uid? You appear to have multiple ones. Do you want to get them all out in an array? You could use map instead of forEach then. – Sebastian Simon Commented Aug 19, 2015 at 3:32
  • Hello @Xufox. I want to get them all out in an array. How will i do it? – ar em Commented Aug 19, 2015 at 3:37
Add a ment  | 

4 Answers 4

Reset to default 4

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.

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

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

      }

This gets all uids into an Array:

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

map creates a new Array based on another one. The function returns whatever you’re looking for (obj.uid).

发布评论

评论列表(0)

  1. 暂无评论