Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda