2016-04-24 37 views
1

Bitgo將所有事務作爲對象提供,並且始終保持在大數組中。在嵌套的條目內部,我們看到第一個TX對象有兩個負值-312084680和-4254539,我只需要最低值。我的代碼工作並記錄所有交易,但只有從一個地址發送貨幣纔是正確的。如果兩個負值的存在,我登錄3筆交易,而不是僅僅2BitGO - 從對象數組中獲取最小值

var allTxs = [ 
    { 
     "id": "e5216ffaaa2a37bcc14380db07f06c85a65bcdc4e1fcab2bd5523f0b8a11bc15", 
     "normalizedHash": "0709c99097386a3c0130f3d6b002acf6a4e37978406704268fc9d308eec4c2b8", 
     "date": "2016-04-17T20:07:03.700Z", 
     "fee": 7440, 
     "inputs": [ 
     { 
      "previousHash": "6d043a06ade4eac5315967c463fcd65deb4ed9bff23ee3e73ff82c9cf72360e9", 
      "previousOutputIndex": 1 
     }, 
     { 
      "previousHash": "b6e566cbee0f23bee7b321eda7f6159a165101e77e7f1e75bd9eb6e31540b391", 
      "previousOutputIndex": 0 
     } 
     ], 
     "outputs": [ 
     { 
      "vout": 0, 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 312500000, 
      "isMine": true, 
      "chain": 0, 
      "chainIndex": 0 
     }, 
     { 
      "vout": 1, 
      "account": "mmRuajWq2xPYQw4gjXz8pQ2fUfJTF7fvYe", 
      "value": 3831779 
     } 
     ], 
     "entries": [ 
     { 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 312500000 
     }, 
     { 
      "account": "muEePZzkRWX3RnLWHxTx6r8T3MMruTgMgg", 
      "value": -312084680 
     }, 
     { 
      "account": "mmRuajWq2xPYQw4gjXz8pQ2fUfJTF7fvYe", 
      "value": 3831779 
     }, 
     { 
      "account": "n47gD5D3XfBG41tWKX4YHNc9gboyWU9yJg", 
      "value": -4254539 
     } 
     ], 
     "confirmations": 677, 
     "pending": false, 
     "instant": false, 
     "blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9", 
     "height": 786821 
    }, 
    { 
     "id": "71fb53e7d70ce27dced2eb327ac544b8f046e66480342ba81533046f3267e6f4", 
     "normalizedHash": "80116b194b58b494d85b2a831815a978ec6f0fe617cfd020880ff1ad76b2bacc", 
     "date": "2016-04-17T20:06:56.474Z", 
     "fee": 4480, 
     "inputs": [ 
     { 
      "previousHash": "1f4145b615f5d067160184a3e9660396f826614c3fcae9abdcb7192c615b843a", 
      "previousOutputIndex": 0 
     } 
     ], 
     "outputs": [ 
     { 
      "vout": 0, 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 625000000, 
      "isMine": true, 
      "chain": 0, 
      "chainIndex": 0 
     }, 
     { 
      "vout": 1, 
      "account": "mpntSJWk116JF58VRDxeMMwr4gC7afVEKt", 
      "value": 390110612 
     } 
     ], 
     "entries": [ 
     { 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 625000000 
     }, 
     { 
      "account": "mqRsJr8szT5XTSLm3CU7i9ePa7kWnC2VWs", 
      "value": -1015115092 
     }, 
     { 
      "account": "mpntSJWk116JF58VRDxeMMwr4gC7afVEKt", 
      "value": 390110612 
     } 
     ], 
     "confirmations": 689, 
     "pending": false, 
     "instant": false, 
     "blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9", 
     "height": 786821 
    } 
]; 


    for (var i=0; i<allTxs.length; i++) { 
    if(allTxs[i].confirmations >= 7) { 
     var tx = allTxs[i]; 
     var value = 0; 
     for (var entriesIndex = 0; entriesIndex < tx.entries.length; ++entriesIndex) { 
     if (tx.entries[entriesIndex].account === wallet.id()) { 
      value += tx.entries[entriesIndex].value; 
     } 
     } 
     for (var receivedFrom = 0; receivedFrom < tx.entries.length; ++receivedFrom){ 
      if (tx.entries[receivedFrom].value < 0){ 
       var verb = (value > 0) ? 'Received' : 'Sent'; 
        console.log(tx.entries[receivedFrom].account + ' : ' + verb + ' ' + (value/1e8).toFixed(8)); 

     } 
     } 
    } 
    } 

這就是魔術發生:

if (tx.entries[receivedFrom].value < 0){ 
        // both values are less than 0 

希望有人認爲這很容易,並賦予了援助之手!

回答

1

爲了提高代碼的可讀性,更改變量的名稱,並使用forEach而不是傳統的for循環,我做了一些更改。還創建了一個名爲minEntry的對象,它存儲最低值的條目。希望這有助於你

var allTransactions = [ 
    { 
     "id": "e5216ffaaa2a37bcc14380db07f06c85a65bcdc4e1fcab2bd5523f0b8a11bc15", 
     "normalizedHash": "0709c99097386a3c0130f3d6b002acf6a4e37978406704268fc9d308eec4c2b8", 
     "date": "2016-04-17T20:07:03.700Z", 
     "fee": 7440, 
     "inputs": [ 
     { 
      "previousHash": "6d043a06ade4eac5315967c463fcd65deb4ed9bff23ee3e73ff82c9cf72360e9", 
      "previousOutputIndex": 1 
     }, 
     { 
      "previousHash": "b6e566cbee0f23bee7b321eda7f6159a165101e77e7f1e75bd9eb6e31540b391", 
      "previousOutputIndex": 0 
     } 
     ], 
     "outputs": [ 
     { 
      "vout": 0, 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 312500000, 
      "isMine": true, 
      "chain": 0, 
      "chainIndex": 0 
     }, 
     { 
      "vout": 1, 
      "account": "mmRuajWq2xPYQw4gjXz8pQ2fUfJTF7fvYe", 
      "value": 3831779 
     } 
     ], 
     "entries": [ 
     { 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 312500000 
     }, 
     { 
      "account": "muEePZzkRWX3RnLWHxTx6r8T3MMruTgMgg", 
      "value": -312084680 
     }, 
     { 
      "account": "mmRuajWq2xPYQw4gjXz8pQ2fUfJTF7fvYe", 
      "value": 3831779 
     }, 
     { 
      "account": "n47gD5D3XfBG41tWKX4YHNc9gboyWU9yJg", 
      "value": -4254539 
     } 
     ], 
     "confirmations": 677, 
     "pending": false, 
     "instant": false, 
     "blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9", 
     "height": 786821 
    }, 
    { 
     "id": "71fb53e7d70ce27dced2eb327ac544b8f046e66480342ba81533046f3267e6f4", 
     "normalizedHash": "80116b194b58b494d85b2a831815a978ec6f0fe617cfd020880ff1ad76b2bacc", 
     "date": "2016-04-17T20:06:56.474Z", 
     "fee": 4480, 
     "inputs": [ 
     { 
      "previousHash": "1f4145b615f5d067160184a3e9660396f826614c3fcae9abdcb7192c615b843a", 
      "previousOutputIndex": 0 
     } 
     ], 
     "outputs": [ 
     { 
      "vout": 0, 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 625000000, 
      "isMine": true, 
      "chain": 0, 
      "chainIndex": 0 
     }, 
     { 
      "vout": 1, 
      "account": "mpntSJWk116JF58VRDxeMMwr4gC7afVEKt", 
      "value": 390110612 
     } 
     ], 
     "entries": [ 
     { 
      "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", 
      "value": 625000000 
     }, 
     { 
      "account": "mqRsJr8szT5XTSLm3CU7i9ePa7kWnC2VWs", 
      "value": -1015115092 
     }, 
     { 
      "account": "mpntSJWk116JF58VRDxeMMwr4gC7afVEKt", 
      "value": 390110612 
     } 
     ], 
     "confirmations": 689, 
     "pending": false, 
     "instant": false, 
     "blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9", 
     "height": 786821 
    } 
]; 

    allTransactions.forEach(function (transaction){  
     var value = 0; 
     var minEntry = { value : 0 }; 
     transaction.entries.forEach(function (entry) { 
     //if (entry.account === wallet.id()) { 
      value += entry.value; 
     //} 
     }); 
     var verb = (value > 0) ? 'Received' : 'Sent'; 
     transaction.entries.forEach(function (entry) { 
      if (entry.value < minEntry.value){ 
      minEntry = entry; 
     } 
     }); 
     if(minEntry.value){ 
     console.log(minEntry.account + ' : ' + verb + ' ' + (value/1e8).toFixed(8)); 
     } 
    }); 
+0

噢,對不起,嘗試transaction.entries.forEach –

+0

改變minValue(最小值)到minEntry,漸漸minValue(最小值)是不確定的。之後沒有錯誤,但console.log(minEntry.account +':'+ verb +''+(value/1e8).toFixed(8));不會被調用。 –

+0

我編輯了代碼,再試一次。在您提供的代碼中,對象錢包不存在。 –