简体   繁体   English

Javascript数组查询

[英]Javascript array of arrays query

I am using highcharts.js to plot a simple column graph using my json data, I have done this before but now I'm plotting with a slightly different dataset (filtered and different format) 我正在使用highcharts.js使用我的json数据绘制一个简单的柱形图,我之前已经这样做过,但是现在我使用略有不同的数据集(已过滤和不同格式)进行绘制

For "processed_json", idea used : http://codeinjs.blogspot.com/2013/07/preprocess-json-data-to-use-in.html 对于“ processed_json”,使用的想法是: http : //codeinjs.blogspot.com/2013/07/preprocess-json-data-to-use-in.html

Currently, my processed_json is : 目前,我的processing_json是:

function plotgraph1(filter1)
        {
            var processed_json = new Array();

            $j.map(filter1, function(obj, i) {
             processed_json.push(obj[0], obj[1]);
             console.log(processed_json)    

            }); 
...................
         }

Console prints : 控制台打印:

["2014-06-14T18:30:00Z", 4044,
 "2014-06-15T18:30:00Z", 5030,
 "2014-06-16T18:30:00Z", 4913,
 "2014-06-17T18:30:00Z", 4985,
 "2014-06-18T18:30:00Z", 4778,
 "2014-06-19T18:30:00Z", 4733,
 "2014-06-20T18:30:00Z", 3960,
 "2014-06-21T18:30:00Z", 4221,  .......]

Opening up gives : 开放给:

0: "2014-06-14T18:30:00Z"
1: 4044
2: "2014-06-15T18:30:00Z"
3: 5030
4: "2014-06-16T18:30:00Z"
5: 4913

.......... and so on .......... 等等

It is storing the date1, metric1 and so on, but I want it to store it in different arrays. 它存储的是date1,metric1等,但是我希望它将其存储在不同的数组中。 Basically, it should be an array of arrays, for each day, a separate array. 基本上,它应该是一个数组数组,每天都有一个单独的数组。 So that highcharts.js can read it properly. 这样highcharts.js才能正确读取它。 Now it's reading the data but, since all dates are in one array it's skipping a date and showing 28 dates instead of 14 days (which is the selected range); 现在,它正在读取数据,但是,由于所有日期都在一个数组中,因此它跳过一个日期并显示28个日期而不是14天(这是所选范围); (double basically) (基本上是两倍)

Earlier and correct "processed_json" is : 较早且正确的“ processed_json”为:

[Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]
0: Array[2]
0: "2014-04-30T18:30:00Z"
1: 200303

I was trying something like this : 我正在尝试这样的事情:

var processed_json1 = [];
            for ( var i = 0; i < processed_json.length; i++)
            {
            processed_json1.push(processed_json[i]);
            }

But, clearly this is wrong and I am wasting too much time. 但是,显然这是错误的,我浪费了太多时间。 Any good direction/suggestion will be highly appreciated to convert my data format to the correct one for highcharts to read it properly. 将我的数据格式转换为正确的格式以供Highcharts正确读取它的任何正确方向/建议将不胜感激。

Giving multiple arguments to push is just short for calling push separately with each argument, it doesn't collect them together in an array. 将多个参数传递给push只是push每个参数分别调用push简称,它不会将它们一起收集在一个数组中。 You need to make them an object first: 您需要首先使它们成为对象:

function plotgraph1(filter1)
{
    var processed_json = $j.each(filter1, function(i, obj) {
        processed_json.push([obj[0], obj[1]]);  

    }); 
    console.log(processed_json)
    ...................
}

DEMO DEMO

I think you are looking for something like the following: 我认为您正在寻找类似以下内容的东西:

            var array = [];
        for(var i=0;i<5;i++){
            array.push({"date":"metric"});
        }
        console.log(array);

Outputs: 输出:

[Object, Object, Object, Object, Object]
0: Object
date: "metric"
1: Object
date: "metric"
2: Object
processed_json.reduce(function(soFar, val, idx) {
    if (idx % 2 == 0) {soFar.push([]);} 
    soFar[soFar.length - 1].push(val); 
    return soFar;
}, []); 
//=> [["2014-06-14T18:30:00Z", 4044], ["2014-06-15T18:30:00Z", 5030], ...]

Is that what you're looking for? 那是您要找的东西吗?

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

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