本文介绍了对象数组按元素分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参阅此示例:
JSON数组
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);
});
有人可以帮助我走上正确的道路吗?
解决方案
您可以将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];
//如果此DtmStamp的属性尚不存在,请创建
if(x [obj.DtmStamp] === undefined)
x [obj.DtmStamp] = [ obj.DtmStamp]; //使用DtmStamp的第一个元素分配一个新数组。
// x将始终是与当前DtmStamp对应的数组。将当前值推送给它。
x [obj.DtmStamp] .push(obj.DblValue);
}
console.log(x); // x现在是由DtmStamp分组的对象。您可以在此轻松将其重新转换为数组。
Please see this example: JsFiddle
Question: I have the following 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} ]
I was trying to group these object by DtmStamp
end up having something like this :
x = [[1358226000000,10,92,45,87],[1358226060000,10,87,45,92], .......]
In other words:
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 ;
Unfortunately, it ends with something I don't want.
Here is what I have tried so far:
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);
});
Can someone help me get on the right path?
解决方案
You can leverage JavaScript objects as a key/value data structure similar to a map. The property name will serve as the key, while the property value will serve as the value. This will allow you to group.
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.
这篇关于对象数组按元素分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!