React JSX Question

non empty array with length=0 in Meteor

const items = [];
Meteor.http.call("GET", url,function(error,result){
$.each(JSON.parse(result.content), function(key, value){
items.push(value)
});
});


Code below return non-empty array items with length 0.
How can I iterate array or extract all values by key to an array?

enter image description here

Answer Source

Meteor.http.call is an asynchronous function with a callback. A quick way I can think to do what you want is the following:

const items = [];
Meteor.http.call("GET", url,function(error,result){  
  $.each(JSON.parse(result.content), function(key, value){
    items.push(value)
  });
  handleItems(items);
});

function handleItems(items) {
  console.log(items.length) // 1
  // Do what you want with the items array here.
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download