本文介绍了JavaScript数组未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript数组的问题传递到另一个对象数组时。我曾尝试一切,我一出​​来就净,但没有什么工作。

I have a problem with javascript array when passing into another object array. I have tried everything I came out on net, but nothing is working.

但问题是在网上获取来自API的数据时,对 dataValues​​ [dataValues​​.length] =(v.potroseno_tekucine); 。其工作与labelValues​​。当我在环其确定警报数据,但需要处理时,它看起来像它的不确定。但是,当我举个例子,警报(dataValues​​.length)之前变种数据,其工作正常。

Problem is in line when fetching data from api on dataValues[dataValues.length] = (v.potroseno_tekucine);. Its working with labelValues. When I alert data in loop its ok, but when it needs to be processed it looks like its undefined. But when I, for example, alert(dataValues.length) before var data, its working ok.

$(document).ready(function () {
var ctx = document.getElementById("PodrumarstvoDetalji").getContext("2d");
var labelValues = [];
var dataValues = [];
$.ajax({
    url: root + "api/StatistikaApi/GetPodrumarstvoChart/?vinograd_id=10",// + $("#vinograd_id").val(),
    type: "Get",
    contentType: 'json',
    dataType: 'json',
    success: function (data) {
        $.each(data, function (k, v) {
            labelValues[labelValues.length] = v.opis;
            dataValues[dataValues.length] = (v.potroseno_tekucine);
        });
    },
    error: function (msg) { alert(msg); }
});
var data = {
    labels: labelValues,
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#ddd",
            pointHighlightFill: "#ddd",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: dataValues
        }/*,
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.2)",
            strokeColor: "rgba(151,187,205,1)",
            pointColor: "rgba(151,187,205,1)",
            pointStrokeColor: "#ddd",
            pointHighlightFill: "#ddd",
            pointHighlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }*/
    ]
};


var myLineChart = new Chart(ctx).Line(data);
});

任何人能帮助我吗?我曾尝试与推动,切片,CONCAT但没有...

Can anyone help me please? I have tried with push, slice, concat but nothing...

推荐答案

Ajax调用是异步的......这是不是意味着数据将会得到任何响应之前设置Ajax请求。

Ajax calls are asynchronous... this mean the data will be set before getting any response from your ajax request.

为了解决这个问题,你需要在阿贾克斯成功的回调函数创建自己的数据:

In order to fix this, you need to create your data in the ajax success callback function:

$.ajax({
    success: function (data) {
        $.each(data, function (k, v) {
            labelValues.push(v.opis);
            dataValues.push(v.potroseno_tekucine);
        });
        var data2 = {
          labels: labelValues,
          //...
        };
        //Insert your logic here to handle data2
    }
});

这篇关于JavaScript数组未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 10:29