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]);
}
          }
        });
  }
Share:
17,458
ar em
Author by

ar em

Updated on June 04, 2022

Comments

  • ar em
    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
      Sebastian Simon almost 9 years
      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.
    • ar em
      ar em almost 9 years
      Hello @Xufox. I want to get them all out in an array. How will i do it?
  • Sebastian Simon
    Sebastian Simon almost 9 years
    Well, data.map(…); creates an array with the uids… do you want to use the Array outside the function within getParticipant — is that what you mean? Just use the array outside of data.map(…); and inside getParticipant(conf_url, function(data) {…});. I guess, this is an asynchronous function, so you kinda have to do it this way.
  • ar em
    ar em almost 9 years
    yes i want to use the array outside the function within getParticipant
  • DarkMakukudo
    DarkMakukudo almost 9 years
    @arem how did you implement it?
  • ar em
    ar em almost 9 years
    ahm . . I want to get the value outside . . foreach . .
  • DarkMakukudo
    DarkMakukudo almost 9 years
    i mean, the global variable one, try also to log your obj variable to see its contents, use console.log
  • ar em
    ar em almost 9 years
    when i access the value outside foreach . . only the last value appear.
  • DarkMakukudo
    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