javascript - Meteor 中长度为 0 的非空数组

标签 javascript reactjs meteor

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

下面的代码返回长度为 0 的非空数组项。 如何迭代数组或通过键将所有值提取到数组?
enter image description here

最佳答案

Meteor.http.call 是一个带有回调的异步函数。我可以想到做你想做的快速方法如下:

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

关于javascript - Meteor 中长度为 0 的非空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42092615/

相关文章:

javascript - 是什么让 Twitter 页面之间的导航如此之快?

javascript - 期望在react中的箭头函数末尾返回一个值

javascript - 如何使用 Material UI 网格组件将一个项目左对齐,另一项右对齐

javascript - 从对象数组动态创建嵌套的 json

javascript - 如何在 Meteor 中使用 Materialize css Tabs?

javascript - 如何在末尾(字母后)按字母顺序对 javascript 数组进行排序,并正确地按数字排序?

javascript - 使用 curl 命令将电子邮件地址添加到 sendgrid

javascript - 根据公共(public)值将对象合并到多维数组

javascript - 当位置(纬度/经度)更改时,缓慢更新 HereMaps DomIcon,重新渲染

mongodb - 使用 Meteor 连接到远程 MongoDB