json数据分组基于node.js中的条件

时间:2016-03-16 12:54:21

标签: json node.js

{
    "table": "alltrades",
    "rows":
    [
        {
            "timestamp": 1458042948000,
            "tid": 4525616,
            "price": 0.03137600,
            "amount": 2.18500000,
            "type": 1
        },
        {
            "timestamp": 1458042949000,
            "tid": 4525617,
            "price": 0.03137600,
            "amount": 2.18500000,
            "type": 1
        },
        {
            "timestamp": 1458042952000,
            "tid": 4525618,
            "price": 0.03137500,
            "amount": 0.00477131,
            "type": 2
        },
        {
            "timestamp": 1458042953000,
            "tid": 4525619,
            "price": 0.03137500,
            "amount": 0.00475697,
            "type": 2
        },
        {
            "timestamp": 1458042953000,
            "tid": 4525620,
            "price": 0.03137500,
            "amount": 0.00037928,
            "type": 2
        },
        {
            "timestamp": 1458042954000,
            "tid": 4525621,
            "price": 0.03137500,
            "amount": 0.00024350,
            "type": 2
        },
        {
            "timestamp": 1458042954000,
            "tid": 4525622,
            "price": 0.03137500,
            "amount": 2.00000000,
            "type": 2
        },
        {
            "timestamp": 1458042954000,
            "tid": 4525623,
            "price": 0.03137600,
            "amount": 0.00018831,
            "type": 1
        },
        {
            "timestamp": 1458042955000,
            "tid": 4525624,
            "price": 0.03137600,
            "amount": 0.00003273,
            "type": 1
        },
        {
            "timestamp": 1458042957000,
            "tid": 4525625,
            "price": 0.03137600,
            "amount": 0.00077868,
            "type": 1
        },
        {
            "timestamp": 1458042958000,
            "tid": 4525626,
            "price": 0.03137600,
            "amount": 0.00000411,
            "type": 1
        }]
}

我有上面的数据我需要根据以下条件对它们进行分组 类型1=buy2=selltid表示交换 如果我们一次购买发生在时间x,另一次购买1秒后,另一次购买1秒后,另一次购买1秒后再3秒没有交易然后再购买 然后我们有4笔交易 我们需要总结一下 然后休息3秒 之后又发生另一次购买,但我们不会添加那个,最好是返回timestamp x 包括duration: 3 seconds总金额和VWAP价格以及type=buytype=sell

所以条件就像那个相同的交换,相同的类型和它们之间的1秒时间间隔然后分组数据 average = (amount*price + amount1*price1)/(amount + amount1)

如果交换不同或类型不同或时间间隔不同,则保持不变。

1 个答案:

答案 0 :(得分:2)

var last = 0;
    var last1 = 0;
    var tmp = _.groupBy(obj["rows"], function(d){ 
        var test2 =  d["timestamp"];
        if(last == 0){
            last = test2;
        }else{
            if((test2 - last1) <= 1000 && (test2 - last1) >= 0){

            }
            else
            {
                last = test2;
            }
        }
        last1 = test2;
      return last;
    });
    // Show the temporary result :o)
    // Now group the result with currency code
    var tmp2 = {};
    _.each(tmp, function(t, unit){
        tmp2[unit] = _.groupBy(t, function(d){
        return d["type"];
        });
    });


    // show the temp result again
    var finalResult = [];
    _.each(tmp2, function(t, unit){
      _.each(t, function(items, currency){
        var total = 0;
        var totalamount = 0;
        var count = 0;
        var VWAP = 0;
        //console.log(items);
        _.each(items, function(item){
          total += item["price"];     // should also * currencyCode?
          totalamount += item["amount"];
          VWAP += item["price"] * item["amount"];
          count++;
        });
        finalResult.push({
            "timestamp" : unit
          , "price" : total
          , "amount" : totalamount
          , "VWAP" : VWAP/count
          , "type" : currency    // Update it yourself :o)
          , "count" : count
        });
      });
    });

    console.log(finalResult);