简体   繁体   English

奇怪的JavaScript数组推送行为

[英]Strange javascript array push behaviour

I wrote a simple code to manipulate a query result to a bidimensional array (matrix) for a google chart datatable. 我编写了一个简单的代码来将查询结果处理为Google图表数据表的二维数组(矩阵)。 I'm getting nut on this strange array.push behaviour: when I push a new row in the matrix this method add the row, but also change the value of all the previus rows! 我对这种奇怪的array.push行为array.push :当我在矩阵中推入新行时,此方法不仅添加行,而且还更改所有先前行的值!

this is the code: 这是代码:

<script src="https://www.gstatic.com/charts/loader.js" type="text/javascript"></script>
<script type="text/javascript">

query is the matrix from the query 查询是查询的矩阵

var qry = [[{label:'Montly', type: 'string'}, {label:'Model', type: 'string'}, {label:'total', type: 'number'}], ['12-2017','California T',parseFloat(13+13)], ['12-2017','458 Speciale',parseFloat(3+2)], ['12-2017','GTC4Lusso',parseFloat(2+6)], ['12-2017','458 Spider',parseFloat(0+1)], ['12-2017','GTC4Lusso T',parseFloat(2+0)], ['12-2017','California',parseFloat(0+2)], ['12-2017','488 Spider',parseFloat(16+7)], ['12-2017','FF',parseFloat(1+3)], ['12-2017','488 GTB',parseFloat(17+10)], ['12-2017','F12berlinetta',parseFloat(3+4)], ['12-2017','458 Italia',parseFloat(0+3)], ['12-2017','F12tdf',parseFloat(1+21)], ['12-2017','LaFerrari Aperta',parseFloat(0+1)], ['12-2017','812 Superfast',parseFloat(2+3)], ['12-2017','Portofino',parseFloat(1+1)], ['11-2017','458 Spider',parseFloat(2+4)], ['11-2017','GTC4Lusso',parseFloat(4+34)], ['11-2017','California T',parseFloat(8+15)], ['11-2017','488 GTB',parseFloat(9+12)], ['11-2017','LaFerrari',parseFloat(0+1)], ['11-2017','458 Speciale',parseFloat(2+2)], ['11-2017','GTC4Lusso T',parseFloat(0+1)], ['11-2017','FF',parseFloat(1+4)], ['11-2017','812 Superfast',parseFloat(3+1)], ['11-2017','California',parseFloat(0+3)], ['11-2017','488 Spider',parseFloat(11+8)], ['11-2017','458 Italia',parseFloat(1+5)], ['11-2017','F12tdf',parseFloat(0+1)], ['11-2017','Portofino',parseFloat(0+1)], ['11-2017','F12berlinetta',parseFloat(3+4)], ['10-2017','458 Italia',parseFloat(0+4)], ['10-2017','California T',parseFloat(9+18)], ['10-2017','California',parseFloat(0+8)], ['10-2017','812 Superfast',parseFloat(1+2)], ['10-2017','F12tdf',parseFloat(1+2)], ['10-2017','Portofino',parseFloat(2+0)], ['10-2017','488 GTB',parseFloat(8+14)], ['10-2017','FF',parseFloat(0+3)], ['10-2017','458 Spider',parseFloat(1+3)], ['10-2017','LaFerrari Aperta',parseFloat(0+1)], ['10-2017','F12berlinetta',parseFloat(5+8)], ['10-2017','458 Speciale',parseFloat(3+2)], ['10-2017','488 Spider',parseFloat(9+7)], ['10-2017','GTC4Lusso',parseFloat(2+4)], ['9-2017','California',parseFloat(0+4)], ['9-2017','458 Speciale Aperta',parseFloat(1+0)], ['9-2017','FF',parseFloat(3+6)], ['9-2017','812 Superfast',parseFloat(2+1)], ['9-2017','458 Italia',parseFloat(1+3)], ['9-2017','GTC4Lusso',parseFloat(2+6)], ['9-2017','F12tdf',parseFloat(1+0)], ['9-2017','LaFerrari',parseFloat(0+1)], ['9-2017','488 GTB',parseFloat(18+13)], ['9-2017','458 Spider',parseFloat(3+2)], ['9-2017','F12berlinetta',parseFloat(4+10)], ['9-2017','458 Speciale',parseFloat(5+1)], ['9-2017','California T',parseFloat(23+37)], ['9-2017','488 Spider',parseFloat(11+14)], ['8-2017','FF',parseFloat(0+1)], ['8-2017','458 Spider',parseFloat(0+1)], ['8-2017','F12berlinetta',parseFloat(0+3)], ['8-2017','GTC4Lusso',parseFloat(0+6)], ['8-2017','488 GTB',parseFloat(0+1)], ['8-2017','California T',parseFloat(0+2)], ['8-2017','458 Italia',parseFloat(0+2)], ['8-2017','California',parseFloat(0+2)], ['7-2017','488 GTB',parseFloat(9+14)], ['7-2017','458 Speciale',parseFloat(1+4)], ['7-2017','California T',parseFloat(14+20)], ['7-2017','GTC4Lusso',parseFloat(1+7)], ['7-2017','California',parseFloat(1+6)], ['7-2017','458 Italia',parseFloat(1+4)], ['7-2017','458 Speciale Aperta',parseFloat(1+0)], ['7-2017','F12berlinetta',parseFloat(3+7)], ['7-2017','FF',parseFloat(2+3)], ['7-2017','458 Spider',parseFloat(1+3)], ['7-2017','488 Spider',parseFloat(10+10)], ['7-2017','F12tdf',parseFloat(0+1)], ['6-2017','488 GTB',parseFloat(7+23)], ['6-2017','458 Italia',parseFloat(2+2)], ['6-2017','812 Superfast',parseFloat(1+0)], ['6-2017','FF',parseFloat(0+3)], ['6-2017','GTC4Lusso',parseFloat(2+16)], ['6-2017','458 Spider',parseFloat(3+6)], ['6-2017','F12tdf',parseFloat(0+2)], ['6-2017','LaFerrari',parseFloat(0+1)], ['6-2017','F12berlinetta',parseFloat(4+9)], ['6-2017','458 Speciale',parseFloat(2+3)], ['6-2017','California T',parseFloat(16+18)], ['6-2017','488 Spider',parseFloat(11+14)], ['5-2017','458 Speciale',parseFloat(4+4)], ['5-2017','458 Italia',parseFloat(0+2)], ['5-2017','F12berlinetta',parseFloat(2+5)], ['5-2017','F12tdf',parseFloat(0+3)], ['5-2017','FF',parseFloat(0+1)], ['5-2017','488 GTB',parseFloat(8+13)], ['5-2017','458 Spider',parseFloat(0+2)], ['5-2017','GTC4Lusso',parseFloat(0+6)], ['5-2017','488 Spider',parseFloat(6+12)], ['5-2017','California T',parseFloat(12+19)], ];

I want to extract the unique models from the column and create the series for the line chart 我想从列中提取唯一模型并为折线图创建系列

function extractColumn(arr, column) {
    function reduction(previousValue, currentValue) {
        previousValue.push(currentValue[column]);
        return previousValue;
    }

    return arr.reduce(reduction, []);
}

var prima = extractColumn(qry,1);
var models = [...new Set(prima)];

with extract column I get all the values of the column 1,and with the spread operator I get the models array, without duplicate. 使用提取列,我得到列1的所有值,使用散布运算符,我得到模型数组,没有重复。

var testa = [];   // the header of the matrix
var tabella = []; // the array for the DataTable

for (i=1; i<models.length; i++) {
    riga[i]=0;
}
testa.push([{label:'Montly', type: 'string'}]);
for (i=1; i<models.length; i++) {
    testa.push([{label:models[i], type:'number'}]);
} // this code create the header objects
tabella.push(testa);      //push the header into the tabella array
console.log(tabella[0]);  //this push is ok!
var mese=qry[1][0];       // first date value
var riga = [];            // array to add as row of the matrix
riga[0]=mese;             // start populating the firs row

the following code should create a new row and append it to the chart table. 以下代码应创建一个新行,并将其附加到图表表。 It checks the date of every row of the query matrix (qry): if it's a new date append the created row to the chart table and start a new row, if not it goes on creating the new row. 它检查查询矩阵(qry)的每一行的日期:如果是新日期,则将创建的行追加到图表表中并开始新行,否则,将继续创建新行。

for (i=1; i<qry.length; i++){             // iterate qry array
    if (mese!=qry[i][0]) {        // check if the date is new
        console.log(riga);                // all the created row are ok!
        tabella.push(riga);               // append the row to the array
        console.table(tabella);           // WEIRD PROBLEM: ALL THE ARRAY ROW ARE CHANGED
        mese=qry[i][0];                   // EVERY CICLE! 
        for (x=1; x<models.length; x++) {
            riga[x]=0;
        }
        riga[0]=mese;                     //reset the new row array and set the date
    } else {
        for (t=0;t<models.length;t++){
            pos = models.indexOf(qry[i][1]);
            if (pos != -1) {
                riga[pos]=qry[i][2]
                }
        }                                // this code populates the new row.
    }
}

The following code create the chart 以下代码创建图表

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
    var data = google.visualization.arrayToDataTable(tabella);

   var options = {
        title: 'Montly Trend Focus on Complaint Type',
        colors: ['#D9D904','#2A55FF','#000000'],
        backgroundColor: {fill:'#FAFAFA',strokeWidth:1},
        chartArea: {width:1800,height:380,left:70},
        curveType: 'none',
        legend: { position: 'bottom' },
        hAxis: {direction:-1},
        annotations: {
            alwaysOutside: true,
            textStyle: {
                fontSize: 12,
                bold: true,
                color: '#000000',
            }
        }
    };

    var chart = new google.visualization.LineChart(document.getElementById('line_chart'));

    chart.draw(data, options);
}

this is an image from my console: 这是来自我的控制台的图像:

在此处输入图片说明

I solved my problem redeclareing the new row in the loop: 我解决了在循环中重新声明新行的问题:

for (i=1; i<qry.length; i++){
    if (mese!=qry[i][0]) {
        tabella.push(riga);
        var riga=[];             // NEW DECLARATION
        mese=qry[i][0];
        for (x=1; x<models.length; x++) {
            riga[x]=0;
        }
        riga[0]=mese;
    }
    else {
        for (t=0;t<models.length;t++){
            pos = models.indexOf(qry[i][1]);
            if (pos != -1){riga[pos]=qry[i][2]}
        }
        }
}

Hope this will be usefull for others. 希望这对其他人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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