BitGO – 从对象数组获取帐户和值

我在做什么:

  • 当一个新的用户注册一个个人钱包时,在Mongo内部生成并存储它们。
  • 后来,我们提出请求,并从Mongo获取所有用户钱包地址,并将它们存储在allDBWallets
  • 每天生成一个新的钱包,它的地址存储在walletId
  • 人们从他们唯一的生成地址向walletId ,我们开始logging和分析所有的walletId交易logging。
 // Check if a wallet has new transactions var allTransactions = [ { "id": "09829738672b3aa5be23775ba7ee81fb2f8c99f386c8731bc9710d376698073b", "normalizedHash": "453e4cb85bf5ad12afa13b017e82f3963a420e6b57c59e50098d0b0a8a850493", "date": "2016-06-13T22:51:55.603Z", "fee": 30000, "inputs": [ { "previousHash": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f", "previousOutputIndex": 4 }, { "previousHash": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f", "previousOutputIndex": 6 }, { "previousHash": "43e5068fddd8c5a2b340524b471aad74755f625ae9d9d6610d132f56c5acbedb", "previousOutputIndex": 0 }, { "previousHash": "c00bd520180c32687ecb9dc00e9d40f918d5b49c752acbe48a39b4b729fc0a03", "previousOutputIndex": 0 } ], "outputs": [ { "vout": 0, "account": "2Mu1BXKcWGxt1HnMdTTLdtPNVV9xmU94Vih", "value": 22890000 }, { "vout": 1, "account": "2N2aohA3TyjLzAsjocHnAHW1zAGtBCvEbPn", "value": 27025087 }, { "vout": 2, "account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH", "value": 705400000, "isMine": true, "chain": 0, "chainIndex": 0 }, { "vout": 3, "account": "2NBJFaWk6WVzwipvAAgQ9aMghhzLu8RL1Bi", "value": 19670000 }, { "vout": 4, "account": "2NFbSredn8i7rGhwAY5vGuBzobkjQRKDy5n", "value": 14420000 } ], "entries": [ { "account": "2NBJFaWk6WVzwipvAAgQ9aMghhzLu8RL1Bi", "value": 19670000 }, { "account": "2N6nyPBXnjUVQiktuAaRd6wkU1FkMgFLja5", "value": -287741933 }, { "account": "2MwgwnZV8sm9nBgjntHjhgEgCP18LtsfEGY", "value": -143298154 }, { "account": "2NFbSredn8i7rGhwAY5vGuBzobkjQRKDy5n", "value": 14420000 }, { "account": "2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3", "value": -203125000 }, { "account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH", "value": 705400000 }, { "account": "2N2aohA3TyjLzAsjocHnAHW1zAGtBCvEbPn", "value": 27025087 }, { "account": "2MvSR1qPDc27w9knqgCkD3vkGw4svxb4fsL", "value": -155270000 }, { "account": "2Mu1BXKcWGxt1HnMdTTLdtPNVV9xmU94Vih", "value": 22890000 } ], "confirmations": 183, "pending": false, "instant": false, "blockhash": "0000000000771a83972ddc9d2f45051c5da52b40e3a4c20259ab700880708293", "height": 870163 }, { "id": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f", "normalizedHash": "ea8540f5e65d1faec9268586d35acfeed97419ac23888a2eef0ed5aa5940aed1", "date": "2016-06-13T03:36:04.369Z", "fee": 11846, "inputs": [ { "previousHash": "92f052103edacef17ae628ed435f64df5740c4a88dd889d57cc04135ace1ed4c", "previousOutputIndex": 0 } ], "outputs": [ { "vout": 0, "account": "2N2jZ3B39oPEVu85V9s74kKwyY4PkoUGuz6", "value": 86370000 }, { "vout": 1, "account": "2NBCtYhx4i13F9xyWTU6WixkknYmdpfJaYw", "value": 113000000 }, { "vout": 2, "account": "2MtBRRoisqfqKzVsvh6QdHWkcUFYaiDbK82", "value": 98630000 }, { "vout": 3, "account": "2N9tXbXxY73KkQNybp5qqFTZu6qN6WRsm7H", "value": 130620000 }, { "vout": 4, "account": "2MvSR1qPDc27w9knqgCkD3vkGw4svxb4fsL", "value": 155270000 }, { "vout": 5, "account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH", "value": 85300000, "isMine": true, "chain": 0, "chainIndex": 0 }, { "vout": 6, "account": "2MwgwnZV8sm9nBgjntHjhgEgCP18LtsfEGY", "value": 143298154 } ], "entries": [ { "account": "2NBCtYhx4i13F9xyWTU6WixkknYmdpfJaYw", "value": 113000000 }, { "account": "2N9tXbXxY73KkQNybp5qqFTZu6qN6WRsm7H", "value": 130620000 }, { "account": "2MwgwnZV8sm9nBgjntHjhgEgCP18LtsfEGY", "value": 143298154 }, { "account": "2N2jZ3B39oPEVu85V9s74kKwyY4PkoUGuz6", "value": 86370000 }, { "account": "2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3", "value": -812500000 }, { "account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH", "value": 85300000 }, { "account": "2MtBRRoisqfqKzVsvh6QdHWkcUFYaiDbK82", "value": 98630000 }, { "account": "2MvSR1qPDc27w9knqgCkD3vkGw4svxb4fsL", "value": 155270000 } ], "confirmations": 370, "pending": false, "instant": true, "instantId": "575e2a24b5bdf3f4069ae3f63b015023", "blockhash": "00000000003c0e6cd52b31dac16dac5b85a3ea1ccb3637b62fb3930ce29f6c0f", "height": 869976 } ] // Daily Generated Wallet var walletId = '2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH'; // Get all Users Addresses from Mongo and store them in allDBWallets var allDBWallets=[]; User.find({}, (err, docs) => { if (err) {console.log('Could not find Wallets in DB');} // console.log(docs); docs.forEach( (address) => { // console.log(address); allDBWallets.push(address.userWallet); }); }); console.log(allDBWallets) [ '2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3', '2NFt8YfydBU5JD9U8Xq2ucbfUp2sP7BjUrh',]; // I'm sure the IF statement is wrong... allTransactions.forEach(function(transaction){ // console.log(transaction); transaction.entries.forEach(function(entry){ for (var i in allDBWallets){ // This is where I fail if (entry.account == allDBWallets[i] && entry.account == walletId) { console.log(entry.account, entry.value); // best result would be allDBWallets[i] with walletId entry.value } } }) }); 

*我希望达到的是这个*

//第一个TX OBJ例子

 { "account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH", // bad address, i need the Users address (2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3) not the walletId address "value": 705400000 // real amount is perfect but can't properly compare and extract users address }, 

希望我find能帮助解释如何validation和提取发送到每日生成地址的用户地址和真实货币的人。

问题是有以下的说法

 if (entry.account == allDBWallets[i] && entry.account == walletId) { 

条件2( entry.account == walletId )只有在entry.account2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH时才为true,在这种情况下,条件1( entry.account == allDBWallets [i] )只有在allDBWallets数组有2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH

而根据上述数据,两种情况都不会同时发生,因此没有得到期望的结果。

因此,您需要更正条件/逻辑以获得所需的输出。

可能的解决scheme

 allTransactions.forEach(function(transaction){ // Check whether there is an entry from any of the wallets in allDBWallets var wallet = transaction.entries.find(function(entry){ return allDBWallets.indexOf(entry.account) > -1; }); // If an entry exists, then find entries for walletId and paint if(wallet) { transaction.entries.forEach(function(entry){ if (entry.account == walletId) { console.log(wallet.account, entry.value); } }) } });