Home > database >  How do I capture elements in my nested objects?
How do I capture elements in my nested objects?

Time:11-30

I have an Array called transactions. The elements in the Array are a collection of objects. The objects in turn have nested elements. How do I capture the nested elements in the objects, in particular, the value of the element called transPayersNumber?

Find below the structure of my Array:

transactions[
    {numberOfMobileTransactions: 200, day: Wednesday,
     phoneNumbers: [{ _id: 60bdf6c18fd22aac8c4b35f2,
                        transPayersNumber: '0705667966',
                        transCounty: 'Kiambo'
                     }] 
     },

    {numberOfMobileTransactions: 120,day: Tuesday, 
     phoneNumbers: [{ _id: 60bdf6c1654622aac8c4b35f2,
                        transPayersNumber: '0747897933',
                        transCounty: 'Kiambo'
                     },
                     { _id: 60bdf6sdtd4622aac8c4b35f2,
                        transPayersNumber: '0747845631',
                        transCounty: 'Nairobi'
                     }] 
     }
]

How do I access the value of the element in the nested Object transactions.phoneNumbers.transPayersNumber?

In a failed attempt to access the value of the transPayersNumber element I tried a variety of codes but the code below is as far as I have gotten without any error messages:

transactions.forEach(transactions => {
    console.log("Transactions: "  transactions.day   " by: "  transactions.phoneNumbers )
});

The code above yeilds:

Transactions: Wednesday by: {
  _id: 60bdf6c18fd22aac8c4b35f2,
  transPayersNumber: '0705667966',
  transCounty: 'Kiambo'
},
Transactions: Tuesday by: {
  _id: 60bdf6c18fd22aac8c4b35f2,
  transPayersNumber: '0705667966',
  transCounty: 'Kiambo'
},
Transactions: Tuesday by: {
  _id: 60bdf6c18fd22aac8c4b35f2,
  transPayersNumber: '0705667966',
  transCounty: 'Kiambo'
}

My desired output would be something similar to the below:

Transactions: Wednesday by: 0705667966
Transactions: Tuesday by: 0747897933, 0747845631

CodePudding user response:

This should work:

const transactions = [
  {
    "numberOfMobileTransactions": 200,
    "day": "Wednesday",
    "phoneNumbers": [
      {
        "_id": "60 bdf6c18fd22aac8c4b35f2",
        "transPayersNumber": "0705667966",
        "transCounty": "Kiambo"
      }
    ]
  },
  {
    "numberOfMobileTransactions": 120,
    "day": "Tuesday",
    "phoneNumbers": [
      {
        "_id": "60 bdf6c1654622aac8c4b35f2",
        "transPayersNumber": "0747897933",
        "transCounty": "Kiambo"
      },
      {
        "_id": "60 bdf6sdtd4622aac8c4b35f2",
        "transPayersNumber": "0747845631",
        "transCounty": "Nairobi"
      }
    ]
  }
];


const transPayersNumbersArray = [];

transactions.map(eachObj => {

  const phoneNumbers = eachObj.phoneNumbers;
  
  if (phoneNumbers && phoneNumbers.length) {
   phoneNumbers.map(eachPhoneNumberObj => {
      transPayersNumbersArray.push(eachPhoneNumberObj.transPayersNumber);
   })
  }

});

console.log('TransPayersNumbersArray ==>', transPayersNumbersArray)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related