Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
How can I access a specific object in my array using JavaScript?

I have created a dynamic array of objects which is created through inquirer. But I cannot figure out how to access a specific object in the array EDIT: this is how the console has logged my array

So for example, how can I access the 2nd Engineer (Mark)?

Keep in mind the array will change depending on the user input

team = [
  Manager {
    name: 'Nicole',
    id: '1',
    email: 'nicole@gmail.com',
    officeNumber: '5'
  },
  Engineer {
    name: 'Zoe',
    id: '2',
    email: 'zoe@gmail.com',
    github: 'zozo'
  },
  Engineer {
    name: 'Mark',
    id: '3',
    email: 'mark@gmail.com',
    github: 'emman'
  },
  Engineer {
    name: 'Joe',
    id: '4',
    email: 'joe@gmail.com',
    github: 'joey'
  }
  Intern {
    name: 'Seb',
    id: '5',
    email: 'seb@gmail.com',
    school: 'UWA'
  }
]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Use find method. If there is no such Mark then find return null.

If you want find Engineer Mark

const result = data.find(x => {
  return x instanceof Engineer && x.name === 'Mark'  
})

[Update] If you want find the second Engineer


const result = data.filter(x => {
  return x instanceof Engineer
})[1]
about 4 years ago · Juan Pablo Isaza Denunciar

0

As Sepehr jozef mentioned the strucure is not that handy. If we take his structure you can find it via the .find Method.

var team = [
{
    name: 'Nicole',
    id: '1',
    email: 'nicole@gmail.com',
    officeNumber: '5',
},
{
    name: 'Zoe',
    id: '2',
    email: 'zoe@gmail.com',
    github: 'zozo'
},
{
    name: 'Mark',
    id: '3',
    email: 'mark@gmail.com',
    github: 'emman'
},
{
    name: 'Joe',
    id: '4',
    email: 'joe@gmail.com',
    github: 'joey'
},
{
    name: 'Seb',
    id: '5',
    email: 'seb@gmail.com',
    school: 'UWA'
 }
]

const mark = team.find(function(teamMember){
    return teamMember.name === "Mark";
})

The variable "mark" contains now the object of the engineer "Mark".

about 4 years ago · Juan Pablo Isaza Denunciar

0

first of all, your structure is wrong.

it should be:

var team = [
    {
        name: 'Nicole',
        id: '1',
        email: 'nicole@gmail.com',
        officeNumber: '5',
    },
    {
        name: 'Zoe',
        id: '2',
        email: 'zoe@gmail.com',
        github: 'zozo'
    },
    {
        name: 'Mark',
        id: '3',
        email: 'mark@gmail.com',
        github: 'emman'
    },
    {
        name: 'Joe',
        id: '4',
        email: 'joe@gmail.com',
        github: 'joey'
    },
    {
        name: 'Seb',
        id: '5',
        email: 'seb@gmail.com',
        school: 'UWA'
    }
]

and to get mark(2) you should use:

team[3].name
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda