Home > Mobile >  How can I loop through array and get value of all it's items
How can I loop through array and get value of all it's items

Time:05-16

I want to loop through an array of objects and get all data of all of objects but only I only get the data of last object I'don't why it skips the first object , please any one could help me

Here is my array

item {
  _id: new ObjectId("627ed71261b2bd692178bdca"),
  sn: 'fN300W15076fd5bg6dwf',
  merchantId: '9000000058800',
  merchantName: '999',
  networkType: '220509',
  __v: 0,
  terminalId: '987654321',
  resultData: [
    {
      _id: new ObjectId("627edb194884d85b48890da8"),
      uniqueID: '68461616865',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '987654321',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'DECLINED',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    },
    {
      _id: new ObjectId("627edb494884d85b48890daa"),
      uniqueID: '71616516',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '987654321',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'DECLINED',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    },
    {
      _id: new ObjectId("627edb5c4884d85b48890dac"),
      uniqueID: '7117117717',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '987654321',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'APPROVED',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    },
    {
      _id: new ObjectId("627edb674884d85b48890dae"),
      uniqueID: '7117117717',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '987654321',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'PENDING',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    }
  ]
}
----------------------------------------------------
item {
  _id: new ObjectId("627ed78561b2bd692178bdd3"),
  sn: 'fN300W15076fd5bg6dwf',
  merchantId: '9000000058800',
  merchantName: '999',
  networkType: '220509',
  __v: 0,
  terminalId: '123456789',
  resultData: [
    {
      _id: new ObjectId("627edb884884d85b48890db0"),
      uniqueID: '97279279',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '123456789',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'GOOD',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    },
    {
      _id: new ObjectId("627edb994884d85b48890db2"),
      uniqueID: '928498452',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '123456789',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'DECLINED',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    },
    {
      _id: new ObjectId("627edba54884d85b48890db4"),
      uniqueID: '6824684294',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '123456789',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'DECLINED',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    },
    {
      _id: new ObjectId("627edbe0ade509d1bd9b5277"),
      uniqueID: '6824684294',
      transactionAmount: '050413',
      currencyCode: '9000000058800',
      terminalId: '123456789',
      transactionDate: '999',
      transactionTime: '220509',
      transactionType: 'DECLINED',
      cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
      onlineRetrievalReferenceNumber: '6161651613516',
      outcome: 'DONE',
      encryptionKeyKCV: '651616161616516',
      transactionEncrypted: 'SUHTHROTRTH161461'
    }
  ]
}
----------------------------------------------------
[
  {
    _id: new ObjectId("627edb884884d85b48890db0"),
    uniqueID: '97279279',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'GOOD',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("627edb994884d85b48890db2"),
    uniqueID: '928498452',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("627edba54884d85b48890db4"),
    uniqueID: '6824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  },
  {
    _id: new ObjectId("627edbe0ade509d1bd9b5277"),
    uniqueID: '6824684294',
    transactionAmount: '050413',
    currencyCode: '9000000058800',
    terminalId: '123456789',
    transactionDate: '999',
    transactionTime: '220509',
    transactionType: 'DECLINED',
    cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
    onlineRetrievalReferenceNumber: '6161651613516',
    outcome: 'DONE',
    encryptionKeyKCV: '651616161616516',
    transactionEncrypted: 'SUHTHROTRTH161461'
  }
]

as you see I get only the array of last object

For who's curious how my code looks like

  async getLogByTPE() {
    let result;
    const data = await this.tpeModel
      .aggregate([
        {
          $lookup: {
            from: 'logs',
            localField: 'terminalId',
            foreignField: 'terminalId',
            as: 'resultData',
          },
        },
        // { $unwind: '$resultData' },
        // {
        //   $project: {
        //     uniqueID: '$logs.currenuniqueIDcyCode',
        //     terminalId: '$logs.terminalId',
        //     transactionAmount: '$logs.transactionAmount',
        //     currencyCode: '$logs.currencyCode',
        //     transactionDate: '$logs.transactionDate',
        //     transactionTime: '$logs.transactionTime',
        //     transactionType: '$logs.transactionType',
        //     cardPAN_PCI: '$logs.cardPAN_PCI',
        //     onlineRetrievalReferenceNumber:
        //       '$logs.onlineRetrievalReferenceNumber',
        //     outcome: '$logs.outcome',
        //     encryptionKeyKCV: '$logs.encryptionKeyKCV',
        //     transactionEncrypted: '$logs.transactionEncrypted',
        //   },
        // },
      ])
      .then((ok) => {
        //start of test
        // for (let i = 0; i <= ok.length; i  ) {
        //   console.log('ok', ok[i].resultData[i]);
        //   data1 = JSON.stringify(ok[i]);
        // }

        //end of test
        // console.log('OOOKKKK', ok);
        ok.forEach((item) => {
          console.log('item', item);
          console.log('----------------------------------------------------');
          //console.log('resultData', item.resultData);
          result = item.resultData;
          // result = item['resultData'].forEach((sub) => {
          //   console.log('sub', sub);
          //   return sub;
          // });
        });
      });

    // let subitemresult;
    // let result = data.forEach((item) => {
    //   console.log('item', item);
    //   console.log(
    //     'item.resultData',
    //     item.resultData.forEach((subitem) => {
    //       console.log('subitem', subitem);
    //     }),
    //   );
    // });
    // console.log('data', data);
    // console.log('result', JSON.stringify(result));
    console.log(result);
    return result;
  }






          
          

ok => my array of objects
item => objects inside of array
resultData => the data I want to get
unfortunately I only resultData of last object as shown above

CodePudding user response:

The result variable where you store the data inside the loop gets overrided on every iteration, so when you return, there is only the last value stored. A quick fix could be assign an empty array in your declaration on result

  let result: any = [];

Then inside the loop you could push to the array result. Change this result = item.resultData; for

  result.push(item.resultData);
  • Related