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

228
Views
Reach element in an object inside an array which is an element in an object inside an array

I've tried to reach an element in object inside an array which is an element in an object inside an array. Look at the code under. The thirdElementInObject has variable length. My plan is to make list element with every object in the mainArray. I haven't made it with the elements in the thirdElementInObject array. Does anyone know how I can do this?

mainArray = [
   {firstElementInObject: xyz,
   secondElementInObject: abc,
   thirdElementInObject: [
     {name: zzz,
     age: aaa,
     },
     {name: cde,
     age: 123,
     },
     ],
},
   {firstElementInObject: xyz,
   secondElementInObject: abc,
   thirdElementInObject: [
     {name: xxx,
     age: yyy,
     },
     {name: abc,
     age: def,
     },
     {name: abc,
     age: def,
     }
     ],
}
]

const parentElementDash = document.querySelector('.mainArrayUl');

const updateHTML = function () {  
    if (mainArray.length > 0) {
        let result = mainArray.map(element => {
            return `
                <li>
                    <p>${element.firstElementInObject}</p>
                    <p>${element.secondElementInObject}</p>

                    /* Here I want to write the name in the 
                   thirdElementInObject. */

                </li>`  
        });
        parentElementDash.innerHTML = result.join('');
    }
}


updateHTML();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • You can get all the names as an array using .map().
  • You can then use .join() to convert the array to string and insert it in the HTML.

Try this

var mainArray = [
    {
        firstElementInObject: 'xyz',
        secondElementInObject: 'abc',
        thirdElementInObject: [
            {name: 'zzz', age: 'aaa',},
            {name: 'cde', age: 123,},
        ],
    },
    {
        firstElementInObject: 'xyz',
        secondElementInObject: 'abc',
        thirdElementInObject: [
            {name: 'xxx', age: 'yyy',},
            {name: 'abc', age: 'def',},
            {name: 'abc', age: 'def',}
        ],
    }
]

const parentElementDash = document.querySelector('.mainArrayUl');

const updateHTML = function () {  
    if (mainArray.length > 0) {
        let result = mainArray.map(element => {
            return `
                <li>
                    <p>${element.firstElementInObject}</p>
                    <p>${element.secondElementInObject}</p>
                    <p>${element.thirdElementInObject.map(({name}) => name).join(', ')}</p>
                </li>`  
        });
        parentElementDash.innerHTML = result.join('');
    }
}

updateHTML();
<ul class="mainArrayUl"></ul>

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!