简体   繁体   中英

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?
在此输入图像描述

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM