object array Group by an element?

前端 未结 5 1024
面向向阳花
面向向阳花 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 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) {
        ...
    }
    

提交回复
热议问题