数组拒绝使用值返回 [英] Array refuses to be returned with values

查看:124
本文介绍了数组拒绝使用值返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从javascript中的persistencejs(这是一个库!)数据库中获取数据。我正在获取值,并推动他们在数组中,我正在做一个forEach循环。在循环中数组正在填充值,我已经使用警报确认了这一点。

但是当我查看循环外部的数组时,它是空的。为什么会这样?



下面是代码(我试过把变量放在函数里面,并试着把它改成this.newDataArray。):

  var newDataArray = new Array(); 
函数getData(){

dataTable.all()。list(function(tasks){
tasks.forEach(function(r){
var tTank1 = r.Tank1;
var tTank2 = r.Tank2;
var tTank3 = r.Tank3;
var tTank4 = r.Tank4;
var tHelicopter = r.Helicopter;
newDataArray.push(tTank1,tTank2,tTank3,tTank4,tHelicopter);
alert(newDataArray);
});
});
alert(newDataArray);
return newDataArray
}

这里我只是测试返回值正在执行的方法:
$ b $ pre $ Data $原型数据=函数(csv){
jQuery.get(csv, function(data){
var result = $ .csv.toArrays(data);
result.shift();
result.forEach(function(r){
var joinedResult = r.join();
fillDB(joinedResult);
});
});
alert(getData());
};

所以我希望有人知道这个答案吗?另外还有一些关于我的代码和/或帖子格式的批评也是值得欢迎的:)

解决方案

问题可能不在于数据jQuery的 get 是一个异步调用,它不会被填充,但是您提醒它太快了。

,并在回调处理之前返回。如果您在获取调用中移动警报,则应该有您的数据。

  jQuery.get(csv,function(data){
var result = $ .csv.toArrays(data);
result ();
result.forEach(function(r){
var joinedResult = r.join();
fillDB(joinedResult);
});
alert(getData());
});
};


I am trying to get data from my persistencejs(this is a library!) database in javascript. I am getting the values and pushing them in the array, I'm doing this in a forEach loop. In the loop the array is being filled with values, I have confirmed this using alerts.

But when I look at the array outside of the loop, it's empty. Why is this?

Here's the code (I've tried putting the variable inside the function, and tried changing it to this.newDataArray as well.):

var newDataArray = new Array();
function getData(){

    dataTable.all().list(function(tasks){
        tasks.forEach(function (r) {
            var tTank1 = r.Tank1;
            var tTank2 = r.Tank2;
            var tTank3 = r.Tank3;
            var tTank4 = r.Tank4;
            var tHelicopter = r.Helicopter;
            newDataArray.push(tTank1, tTank2, tTank3, tTank4, tHelicopter);
            alert(newDataArray);
        });
    });
    alert(newDataArray);
    return newDataArray
}

Here I am just testing the return value in a method that's being executed:

Data.prototype.fetchData = function(csv){
    jQuery.get(csv, function(data) {
        var result = $.csv.toArrays(data);
        result.shift();
        result.forEach(function (r) {
            var joinedResult = r.join();
            fillDB(joinedResult);
        });
    });
    alert(getData());
};

So I'm hoping anyone knows the answer to this? Also other criticism about my code and/or post format are welcome as well :)

解决方案

The problem is probably not that the data isn't being populated, but that you're alerting it too soon.

jQuery's get is an asynchronous call, and it returns before the callback has processed. If you move the alert inside the get call, you should have your data.

Data.prototype.fetchData = function(csv){
    jQuery.get(csv, function(data) {
        var result = $.csv.toArrays(data);
        result.shift();
        result.forEach(function (r) {
            var joinedResult = r.join();
            fillDB(joinedResult);
        });
        alert(getData());
    });
};

这篇关于数组拒绝使用值返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆