object array Group by an element?

前端 未结 5 993
面向向阳花
面向向阳花 2020-12-30 14:42

Please see this example: JsFiddle

Question: I have the following JSON Array

y= [ {\"LngTrend\":15,\"DblValue\":10,\"DtmStamp\":13582260         


        
相关标签:
5条回答
  • 2020-12-30 14:50

    Your code pushes empty trendArrays even if they are not filled (index % 4 != 0). Instead, use this:

    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, it might fit better if you just grouped the trendArrays 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 讨论(0)
  • 2020-12-30 15:04
     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 讨论(0)
  • 2020-12-30 15:06

    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 an object, that the array elements are in DtmStamp order.

    //To loop through x
    for(i in x) {
        ...
    }
    
    0 讨论(0)
  • 2020-12-30 15:10

    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.
    
    0 讨论(0)
  • 2020-12-30 15:13

    You should use a hash. A hash will allow you to easily index all of the DblValue values by DtmStamp. Here is a full 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 on the values you want to group by.

    Result:

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

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

    0 讨论(0)
提交回复
热议问题