Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
¿Cómo capturo elementos en mis objetos anidados?

Tengo una matriz llamada transactions . Los elementos del Array son una colección de objetos. Los objetos a su vez tienen elementos anidados. ¿Cómo capturo los elementos anidados en los objetos, en particular, el valor del elemento llamado transPayersNumber ?

Encuentre a continuación la estructura de mi matriz :

 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' }] } ]

¿Cómo accedo al valor del elemento en el objeto anidado transactions.phoneNumbers.transPayersNumber ?

En un intento fallido de acceder al valor del elemento transPayersNumber , probé una variedad de códigos, pero el código a continuación es lo más lejos que he llegado sin ningún mensaje de error:

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

El código anterior produce:

 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' }

Mi salida deseada sería algo similar a lo siguiente:

 Transactions: Wednesday by: 0705667966 Transactions: Tuesday by: 0747897933, 0747845631
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Esto debería funcionar:

 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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!