比较两个对象数组并过滤结果

时间:2016-06-22 20:15:45

标签: javascript node.js validation if-statement foreach

示例:

const depositAddress = '2NBXPR5PRtW8xBRuDnWXBDXqHYpDPupWnhG';

DBarray1.forEach( (tx) => {
  console.log(tx);
})

TXarray2.forEach( (sim) => {
  console.log(sim);
});

DBarray1 = [
    {
       "_id": "575e2b7875a402111900ba8f",
       "username": "aleluia@gmail.com",
       "playerWallet": "2NFt8YfydBU5JD9U8Xq2ucbfUp2sP7BjUrh",
       "User_Profile": {
          "TXHash": [
           "7fbe28f75412f19dfd123a08ce03c33c302aa13d1e68d38ab8cb4c7418777f8e"
          ]
       }
    },
    {
       "_id": "575e2946b909906a17ea65b9",
       "username": "super@user.com",
       "playerWallet": "2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3",
       "User_Profile": {
          "TXHash": [
             "cf948340a40d3302303dfb3710cfce37bb1cd156dcb6c74561fdc71c0a8fc30b",
             "6219def49d2e8284a6031f4c7e05e21adf756d38904e6359bd7844ae14c75a50"
          ]
       }
    }
 ] // end console.log(DBarray1);


TXarray2 = [
    {
       "id": "cf948340a40d3302303dfb3710cfce37bb1cd156dcb6c74561fdc71c0a8fc30b",
       "normalizedHash": "f62af1a61c7eb569c1a171ad23c70bc218bd7244c9c5c92cf7d98638314fbbc5",
       "date": "2016-06-21T04:11:18.541Z",
       "fee": 6280,
       "inputs": [
          {
             "previousHash": "2660fb761354671912b0cea6427e9ee91a98a507e5f1408865a6058b566b508c",
             "previousOutputIndex": 0
          },
          {
             "previousHash": "ce3ef138c11ea4d1766cce52ccf5f1e91790bc03b56561b0eb669041bae4e1a3",
             "previousOutputIndex": 0
          }
       ],
       "outputs": [
          {
             "vout": 0,
             "account": "2N92kApgroS6CTVuTajtjWtpcAZpUiyQoDT",
             "value": 861003
          },
          {
             "vout": 1,
             "account": "2NBXPR5PRtW8xBRuDnWXBDXqHYpDPupWnhG",
             "value": 3100000,
             "isMine": true,
             "chain": 0,
             "chainIndex": 0
          }
       ],
       "entries": [
          {
             "account": "2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3",
             "value": -3967283
          },
          {
             "account": "2N92kApgroS6CTVuTajtjWtpcAZpUiyQoDT",
             "value": 861003
          },
          {
             "account": "2NBXPR5PRtW8xBRuDnWXBDXqHYpDPupWnhG",
             "value": 3100000
          }
       ],
       "confirmations": 70,
       "pending": false,
       "instant": true,
       "instantId": "5768be65427689eb06e597559c7e6cf0",
       "blockhash": "00000000002d9fb51c7c3c1607fe062eff686aa6be657a59fee6c3044963897d",
       "height": 872152
    },
    {
       "id": "6219def49d2e8284a6031f4c7e05e21adf756d38904e6359bd7844ae14c75a50",
       "normalizedHash": "179a4466fdfc5470e99e43aa177d43aa4f09e3a06760fd5bebffdda080d4407f",
       "date": "2016-06-21T04:13:23.650Z",
       "fee": 9096,
       "inputs": [
          {
             "previousHash": "5d2879a79ea3d0dcb50049ef9ca46ef7e8d82caf2073a299a6cd0332add404c8",
             "previousOutputIndex": 1
          },
          {
             "previousHash": "d75288e69a3fc2edd534ddcd845af6a280a27af58013ae82828c8a8f813829c1",
             "previousOutputIndex": 0
          },
          {
             "previousHash": "eea4f9b274708b60c1b030203543a155857bc54aa11055ada04aceee706f96b9",
             "previousOutputIndex": 0
          }
       ],
       "outputs": [
          {
             "vout": 0,
             "account": "2NBXPR5PRtW8xBRuDnWXBDXqHYpDPupWnhG",
             "value": 2000000,
             "isMine": true,
             "chain": 0,
             "chainIndex": 0
          },
          {
             "vout": 1,
             "account": "2MzFTm5jnCDiAapjNnyVgZAJrXMKfQ74esV",
             "value": 9859
          }
       ],
       "entries": [
          {
             "account": "2MzcwVFKF274bMNT5tNEDY7Ua7bAgvFUdu9",
             "value": -35316
          },
          {
             "account": "2MzFTm5jnCDiAapjNnyVgZAJrXMKfQ74esV",
             "value": 9859
          },
          {
             "account": "2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3",
             "value": -1983639
          },
          {
             "account": "2NBXPR5PRtW8xBRuDnWXBDXqHYpDPupWnhG",
             "value": 2000000
          }
       ],
       "confirmations": 70,
       "pending": false,
       "instant": true,
       "instantId": "5768bee2b5bdf3f406e7db035aef016a",
       "blockhash": "00000000002d9fb51c7c3c1607fe062eff686aa6be657a59fee6c3044963897d",
       "height": 872152
    },
    {
       "id": "7fbe28f75412f19dfd123a08ce03c33c302aa13d1e68d38ab8cb4c7418777f8e",
       "normalizedHash": "b4f1974dccde5ea9dfb0abcd7d4a6f3f14995d9dd422aa7d2a9078229ff18ff4",
       "date": "2016-06-21T03:39:25.034Z",
       "fee": 3465,
       "inputs": [
          {
             "previousHash": "97fbb6ed8646f7ce9ed10a4230a70348151d5b6b208ad068e3a1a3fddae2dc0e",
             "previousOutputIndex": 2
          }
       ],
       "outputs": [
          {
             "vout": 0,
             "account": "2NBXPR5PRtW8xBRuDnWXBDXqHYpDPupWnhG",
             "value": 111200000,
             "isMine": true,
             "chain": 0,
             "chainIndex": 0
          },
          {
             "vout": 1,
             "account": "2NFJnLrhsCDfG3ooQvGC169gnzBabtRgV2y",
             "value": 244246993
          }
       ],
       "entries": [
          {
             "account": "2NCGUnwpNgaJbhMZKLJcBrWvZhWnai5PjVC",
             "value": -355450458
          },
          {
             "account": "2NFJnLrhsCDfG3ooQvGC169gnzBabtRgV2y",
             "value": 244246993
          },
          {
             "account": "2NBXPR5PRtW8xBRuDnWXBDXqHYpDPupWnhG",
             "value": 111200000
          }
       ],
       "confirmations": 77,
       "pending": false,
       "instant": false,
       "blockhash": "0000000000509dbc80cc3d86cdb10ce8e87ab7867c6775a9b00ca904fbe70da7",
       "height": 872145
    }
 ]// end console.log(TXarray2);

我们如何检查TXarray2.id哪个是交易ID,如果它与用户在DBarray1.User_Profile.TXHash内的付款匹配,例如cf948340a40d3302303dfb3710cfce37bb1cd156dcb6c74561fdc71c0a8fc30b

我想知道付款的每位TXarray2.id。我尝试用promises做到这一点,当我回到家时我会分享一些代码,但我确信可以通过所有用户的async一个接一个地完成异步并记录谁今天向这个钱包付款。我尝试使用array.find()方法检查TXHash内部但是失败了,我还没有完全掌握许多原型方法......

希望有人已经认为这是他在公园散步,并找到了一个更好的解决方案来验证这种东西。我会接受任何答案,即使是lodash,maping,任何东西。 TY !!!

3 个答案:

答案 0 :(得分:2)

您迭代TXArray2并在DBarray1中为transactionId执行查找。 我喜欢使用mapfilter等原生数组方法,所以我会使用类似下面的内容:

const result = TXarray2.map(tx => ({
  transaction: tx,
  user: DBarray1.filter(user => user.User_Profile.TXHash.indexOf(tx.id) > -1)[0]
}));

在此示例中,result是一个数组,其中每个元素都包含一个事务和匹配的用户。

答案 1 :(得分:1)

如果您已拥有所有交易和用户数据,您可以这样做吗?

Table/View                      Schema          Type  Creation time             
------------------------------- --------------- ----- --------------------------
AA                              DB2INST1        T     2016-06-07-14.23.08.927146
MYNEWTABLE                      DB2INST1        T     2016-06-07-14.29.50.859806
academy                         DB2INST1        T     2016-06-07-17.05.27.510905

答案 2 :(得分:1)

这里最好的方法是使用Array.indexOf(如果你想使用回调,则使用Array.findIndex),如果一个条目不在数组中,则返回-1。

这是同步变体:

var paid = [];

$("#myButton").click(function(){
    var name = $("#nameID").val();
    var email = $("#emailID").val();
    // Save the name in localStorage.
    localStorage.setItem('name', name);
    // Save the email in localStorage.
    localStorage.setItem('email', email);
    init();
    document.location.replace("page2.html");
});



function init() {

            var stmt = new ADL.XAPIStatement(
                new ADL.XAPIStatement.Agent('mailto:email', name),
                new ADL.XAPIStatement.Verb('http://adlnet.gov/expapi/verbs/completed', 'completed'),
                new ADL.XAPIStatement.Activity('act:http://johnmenken.blogspot.com/2016/01/practice-sending-xapi-statements.html', 'Diamond in the Rough',
                    'Article on curation by Ben Betts and Allison Anderson.')   
            );
            //generates a unique ID for the statement
            stmt.generateId();
            //Other contextual information about the Activity
            stmt.addOtherContextActivity( new ADL.XAPIStatement.Activity('Category:Curation') );
            //Registration: An instance of a learner experiencing a particular Activity.
            stmt.generateRegistration();

            ADL.XAPIWrapper.changeConfig({
                'endpoint': 'https://lrs.adlnet.gov/xapi/',
                'user': 'xapi-tools',
                'password': 'xapi-tools',
                'auth': 'xapi-tools'
            });

            ADL.XAPIWrapper.sendStatement(stmt);
}