请参阅此示例:JsFiddle
问题:我有以下JSON Array
y= [ {"LngTrend":15,"DblValue":10,"DtmStamp":1358226000000},
{"LngTrend":16,"DblValue":92,"DtmStamp":1358226000000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226000000},
{"LngTrend":18,"DblValue":87,"DtmStamp":1358226000000},
{"LngTrend":15,"DblValue":10,"DtmStamp":1358226060000},
{"LngTrend":16,"DblValue":87,"DtmStamp":1358226060000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226060000},
{"LngTrend":18,"DblValue":92,"DtmStamp":1358226060000} ]
我试图通过DtmStamp
对这些对象进行分组,最终得到类似的内容:
x = [[1358226000000,10,92,45,87],[1358226060000,10,87,45,92], .......]
换句话说:
x[0][0] = y[0].DtmStamp ;
x[0][1] = y[0].LngTrend ;
x[0][2] = y[1].LngTrend ;
x[0][3] = y[2].LngTrend ;
x[0][4] = y[3].LngTrend ;
不幸的是,它以我不想要的东西结束。
这是我到目前为止所尝试的内容:
var dataTrendArray = [];
$.each(x, function (index, value) {
var trendArray = [];
if (index % 4 == 0) {
trendArray.push(x[index].DtmStamp);
for (var i = 0; i < 4; i++) {
index = eval(index + i);
trendArray.push(x[index].DblValue);
}
}
console.log(trendArray) ;
dataTrendArray.push(trendArray);
});
有人可以帮助我走上正确的道路吗?
答案 0 :(得分:17)
您可以将JavaScript对象用作类似于地图的键/值数据结构。属性名称将用作键,而属性值将用作值。这将允许您分组。
var y = [
{"LngTrend":15,"DblValue":10,"DtmStamp":1358226000000},
{"LngTrend":16,"DblValue":92,"DtmStamp":1358226000000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226000000},
{"LngTrend":18,"DblValue":87,"DtmStamp":1358226000000},
{"LngTrend":15,"DblValue":10,"DtmStamp":1358226060000},
{"LngTrend":16,"DblValue":87,"DtmStamp":1358226060000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226060000},
{"LngTrend":18,"DblValue":92,"DtmStamp":1358226060000},
];
var x = {};
for (var i = 0; i < y.length; ++i) {
var obj = y[i];
//If a property for this DtmStamp does not exist yet, create
if (x[obj.DtmStamp] === undefined)
x[obj.DtmStamp] = [obj.DtmStamp]; //Assign a new array with the first element of DtmStamp.
//x will always be the array corresponding to the current DtmStamp. Push a value the current value to it.
x[obj.DtmStamp].push(obj.DblValue);
}
console.log(x); //x is now an object grouped by DtmStamp. You can easily turn it back into an array here.
答案 1 :(得分:8)
您应该使用hash。通过哈希,您可以轻松地将所有DblValue
值编入索引DtmStamp
。这是一个完整的工作示例:
var y = [ {"LngTrend":15,"DblValue":10,"DtmStamp":1358226000000},
{"LngTrend":16,"DblValue":92,"DtmStamp":1358226000000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226000000},
{"LngTrend":18,"DblValue":87,"DtmStamp":1358226000000},
{"LngTrend":15,"DblValue":10,"DtmStamp":1358226060000},
{"LngTrend":16,"DblValue":87,"DtmStamp":1358226060000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226060000},
{"LngTrend":18,"DblValue":92,"DtmStamp":1358226060000} ];
var x = {};
var i = 0;
while(i++ < y.length) {
var key = y[i].DtmStamp.toString();
if (typeof(x[key]) == "undefined") x[key] = [];
x[key].push(y[i].DblValue);
}
alert(JSON.stringify(x));
关键是对要分组的值使用哈希值。
结果:
{
"1358226060000": [
92,
45,
87,
10
],
"1358226000000": [
87,
45,
92,
10
]
}
如果您想防止重复,可以将if/then
逻辑与indexOf()
一起添加。
答案 2 :(得分:0)
即使代码未填充(trendArray
),您的代码也会推送空index % 4 != 0
s。相反,使用这个:
var dataTrendArray = [];
for (var i = 0; i < x.length; i += 4) {
var trendArray = [ x[i].DtmStamp ];
for (var j = i, l = Math.max(i + 4, x.length); j < l; j++) {
trendArray.push(x[j].DblValue);
}
// console.log(trendArray) ;
dataTrendArray.push(trendArray);
}
但是,如果您只使用DtmStamp
作为键将trendArrays分组到一个对象中,它可能会更合适:
var dataTrend = {};
for (var i=0; i<x.length; i++) {
var key = x[i].DtmStamp;
if (key in dataTrend)
dataTrend[key].push(x[i].DblValue);
else
dataTrend[key] = [ x[i].DblValue ];
}
答案 3 :(得分:0)
您可以构建一个由DtmStamp索引的稀疏数组。
var x = [];
$.each(y, function(i, obj) {
var s = obj.DtmStamp;
if(!x[s]) x[s] = [];
x[s].push(obj.DblValue);
});
//x is now a sparse array, indexed by DtmStamp
这比对象更有优势,即数组元素的顺序为DtmStamp。
//To loop through x
for(i in x) {
...
}
答案 4 :(得分:0)
var y = [
{"LngTrend":15,"DblValue":10,"DtmStamp":1358226000000},
{"LngTrend":16,"DblValue":92,"DtmStamp":1358226000000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226000000},
{"LngTrend":18,"DblValue":87,"DtmStamp":1358226000000},
{"LngTrend":15,"DblValue":10,"DtmStamp":1358226060000},
{"LngTrend":16,"DblValue":87,"DtmStamp":1358226060000},
{"LngTrend":17,"DblValue":45,"DtmStamp":1358226060000},
{"LngTrend":18,"DblValue":92,"DtmStamp":1358226060000},
];
var x = {};
for(var k in y){
if(x[y[k]["DtmStamp"]] == undefined)
x[y[k]["DtmStamp"]] = [];
x[y[k]["DtmStamp"]].push(y[k]["DblValue"])
}
alert(JSON.stringify(x))
console.log(x);