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

130
Views
displaying the firstName from an object and array

I have an array called persons, that contains objects and arrays within. I'm trying to get all the first names in the siblings array to display. I have a for loop that is displaying John and Maria, but when i call the siblings, the only names that display are Jenny and Jose. I tried different things, Is there a way to display all the firstnames within the siblings array?

var persons = [{
  firstName:"John", 
  lastName:"Doe", 
  age:50, 
  eyeColor:"blue",
  weight: 180,
  siblings: [
    {firstName:"Jenny",
     age:43,
     eyecolor:"brown",},
   {firstName:"Jim",
     age:35,
     eyecolor:"green",},
   {firstName:"Joe",
     age:29,
     eyecolor:"black",},
     ] },

  {firstName:"Maria", 
  lastName:"Lopez", 
  age:19, 
  eyeColor:"brown",
  weight: 120,
  siblings: [
    {firstName:"George",
     age:25,
     eyecolor:"brown",},
   {firstName:"Jose",
     age:20,
     eyecolor:"green",},
    ]},
  
];

  for(i=0; i<persons.length; i++){
    console.log(persons[i].firstName);
    console.log(persons[i].siblings[i].firstName);
  }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use the Array.forEach() method to easily loop through the array items. Then just console.log the firstname.

var persons = [{
    firstName: "John",
    lastName: "Doe",
    age: 50,
    eyeColor: "blue",
    weight: 180,
    siblings: [{
        firstName: "Jenny",
        age: 43,
        eyecolor: "brown",
      },
      {
        firstName: "Jim",
        age: 35,
        eyecolor: "green",
      },
      {
        firstName: "Joe",
        age: 29,
        eyecolor: "black",
      },
    ]
  },

  {
    firstName: "Maria",
    lastName: "Lopez",
    age: 19,
    eyeColor: "brown",
    weight: 120,
    siblings: [{
        firstName: "George",
        age: 25,
        eyecolor: "brown",
      },
      {
        firstName: "Jose",
        age: 20,
        eyecolor: "green",
      },
    ]
  },

];

persons.forEach(person => console.log(person.firstName))

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!