从json数据创建自定义对象数组

时间:2017-03-25 19:15:37

标签: javascript arrays json highstock

我有一个json对象数组,如:

[{"SensorID":2,"Temperature":34.0,"Humidity":56.0,"Carbon":87.0,"DateTime":"2017-03-08T10:07:00","ID":10},{"SensorID":2,"Temperature":34.0,"Humidity":43.0,"Carbon":87.0,"DateTime":"2017-03-09T12:00:00","ID":12},{"SensorID":2,"Temperature":23.0,"Humidity":89.0,"Carbon":65.0,"DateTime":"2017-03-09T12:00:00","ID":13},{"SensorID":2,"Temperature":34.0,"Humidity":56.0,"Carbon":87.0,"DateTime":"2017-03-08T10:07:00","ID":16},{"SensorID":2,"Temperature":87.0,"Humidity":34.0,"Carbon":23.0,"DateTime":"2017-03-08T10:07:00","ID":18},{"SensorID":2,"Temperature":44.0,"Humidity":67.0,"Carbon":43.0,"DateTime":"2017-03-08T10:07:00","ID":20},{"SensorID":2,"Temperature":100.0,"Humidity":100.0,"Carbon":100.0,"DateTime":"2017-03-08T10:07:00","ID":21},{"SensorID":2,"Temperature":70.0,"Humidity":20.0,"Carbon":50.0,"DateTime":"2017-03-08T10:07:00","ID":22}]

现在我想要一个像

这样的数组
[[Date.UTC(2015,1,13),0.8783],
    [Date.UTC(2015,1,15),0.8774],
    [Date.UTC(2015,1,16),0.8807],
    [Date.UTC(2015,1,17),0.8762],
    [Date.UTC(2015,1,18),0.8774],
    [Date.UTC(2015,1,19),0.8798],
    [Date.UTC(2015,1,20),0.8787],]]

我需要在highstock中使用第二个数组如何从第一个数组

创建第二种数组格式

2 个答案:

答案 0 :(得分:0)

这似乎是使用map的问题。基本思想是将一个函数应用于列表中的每个元素,同时创建一个新列表。

xs  = [{"SensorID":2,"Temperature":34.0,"Humidity":56.0,"Carbon":87.0,"DateTime":"2017-03-08T10:07:00","ID":10} ... ]

otherArray = xs.map(function(x) {
   // or whatever logic you need...
   return Date(x.DateTime);
})

答案 1 :(得分:0)

我试过这个。好吗?

var array1 = [{"SensorID":2,"Temperature":34.0,"Humidity":56.0,"Carbon":87.0,"DateTime":"2017-03-08T10:07:00","ID":10},{"SensorID":2,"Temperature":34.0,"Humidity":43.0,"Carbon":87.0,"DateTime":"2017-03-09T12:00:00","ID":12},{"SensorID":2,"Temperature":23.0,"Humidity":89.0,"Carbon":65.0,"DateTime":"2017-03-09T12:00:00","ID":13},{"SensorID":2,"Temperature":34.0,"Humidity":56.0,"Carbon":87.0,"DateTime":"2017-03-08T10:07:00","ID":16},{"SensorID":2,"Temperature":87.0,"Humidity":34.0,"Carbon":23.0,"DateTime":"2017-03-08T10:07:00","ID":18},{"SensorID":2,"Temperature":44.0,"Humidity":67.0,"Carbon":43.0,"DateTime":"2017-03-08T10:07:00","ID":20},{"SensorID":2,"Temperature":100.0,"Humidity":100.0,"Carbon":100.0,"DateTime":"2017-03-08T10:07:00","ID":21},{"SensorID":2,"Temperature":70.0,"Humidity":20.0,"Carbon":50.0,"DateTime":"2017-03-08T10:07:00","ID":22}];
var arraye =[];
for (i in array1)
{
arraye.push([Date.parse(array1[i].DateTime), array1[i].Carbon]);

}