An array of objects by element?

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 tried to group this object using DtmStamp , the result is 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, this ends with what 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 on the right track?

+6
source share
5 answers

You can use JavaScript objects as a key / value data structure similar to a map. The name of the property will serve as the key, and the value of the property 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. 
+17
source

You must use hash . The hash makes it easy to index all DblValue values ​​on DtmStamp . Here is a complete working example:

jsFiddle

 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)); 

The key is to use a hash for the values ​​you want to group.

Result:

 { "1358226060000": [ 92, 45, 87, 10 ], "1358226000000": [ 87, 45, 92, 10 ] } 

If you want to prevent duplicates, you can do this by adding if/then logic in conjunction with indexOf() .

+8
source

Your code will empty empty trendArray , even if they are not filled ( index % 4 != 0 ). Use this instead:

 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); } 

However, this might fit better if you simply grouped trend charts into an object using the DtmStamp as keys:

 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 ]; } 
0
source

You can build a sparse array indexed by 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 

This has the advantage over the object that the elements of the array are in DtmStamp order.

 //To loop through x for(i in x) { ... } 
0
source
  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); 

See http://plnkr.co/edit/511sKSdzHGYuvpYqKCPD?p=preview

0
source

All Articles