简体   繁体   English

将一维数组转换为二维数组

[英]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. 我有从ajax函数获得的数组,该函数包含要替换为图表的信息。

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): 并使用类似这样的方法(请注意,我不使用highcharts,但我认为问题中使用空对象的方式不正确,第一个参数是空对象):

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

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

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