简体   繁体   中英

convert one dimension array into two dimension array

I want to implement plugin chart into my code, the chart code is like

 chart = new Highcharts.Chart({
            },
            series: [{
                type: 'pie',
                name: 'criterin',
                data: [
                    ['Firefox',   45.0],
                    ['IE',       26.8],
                    {
                        name: 'Chrome',
                        y: 12.8,
                        sliced: true,
                        selected: true
                    },
                    ['Safari',    8.5],
                    ['Opera',     6.2],
                    ['Others',   0.7]
                ]
            }]
        });

I have array that I got from ajax function which contains the information that I want to replace into the chart.

for example, I alert sample array and result like:

post work,0.64,quality,0.35

How can I use my array to integrate with chart code.

So, if I'm understanding you correctly, you have an array that looks like this

['post work',0.64,'quality',0.35]

And you want it to look like this:

[
    ['post work', 0.64],
    ['quality', 0.35]
]

Something like this should work

function array1Dto2D(sourceArray) {
    var array2D = [];
    for (var i=0; i < sourceArray.length - 1; i+=2) {
        var pair = [sourceArray[i], sourceArray[i+1]];
        array2D.push(pair);
    }
    return array2D;
}    

and be used something like this (Note I don't use highcharts, but I don't think the way you had it in your question was right, with the empty object as a first argument):

 chart = new Highcharts.Chart({
        series: [{
            type: 'pie',
            name: 'criterin',
            data: array1Dto2D(my1dsourcedata)
        }]
    });

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