0

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: '[email protected]',
    officeNumber: '5'
  },
  Engineer {
    name: 'Zoe',
    id: '2',
    email: '[email protected]',
    github: 'zozo'
  },
  Engineer {
    name: 'Mark',
    id: '3',
    email: '[email protected]',
    github: 'emman'
  },
  Engineer {
    name: 'Joe',
    id: '4',
    email: '[email protected]',
    github: 'joey'
  }
  Intern {
    name: 'Seb',
    id: '5',
    email: '[email protected]',
    school: 'UWA'
  }
]
3
  • Ehm, that's not actually valid code? Did you run this? Aside from that, you could use a for loop to check each object in the team array and look for the object where name === "Mark". That way it will keep working if more objects are added. Commented Oct 31, 2021 at 10:34
  • 1
    It’s not wrong, it’s just not an object literal. This is very likely how a console will log it. So what are Manager, Engineer, and Intern? Classes? Then do team.filter((obj) => obj instanceof Engineer)[1]. Commented Oct 31, 2021 at 10:39
  • Sorry yes that is how the console has logged it. Apologies I am very new to coding! Thanks for the help :) Commented Oct 31, 2021 at 10:42

3 Answers 3

2

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]
Sign up to request clarification or add additional context in comments.

3 Comments

“how can I access the 2nd Engineer (Mark)?” reads like “How to find the second item which is an Engineer, which happens to have a name of "Mark" in this example”, not like “How to find the first item which is both an Engineer and has a name of "Mark".
@SebastianSimon thanks, I have updated answer. Question is not clear enough.
This is perfect thankyou! Still learning so unsure of how to write things correctly yet. Appreciate the options
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: '[email protected]',
    officeNumber: '5',
},
{
    name: 'Zoe',
    id: '2',
    email: '[email protected]',
    github: 'zozo'
},
{
    name: 'Mark',
    id: '3',
    email: '[email protected]',
    github: 'emman'
},
{
    name: 'Joe',
    id: '4',
    email: '[email protected]',
    github: 'joey'
},
{
    name: 'Seb',
    id: '5',
    email: '[email protected]',
    school: 'UWA'
 }
]

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

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

Comments

-1

first of all, your structure is wrong.

it should be:

var team = [
    {
        name: 'Nicole',
        id: '1',
        email: '[email protected]',
        officeNumber: '5',
    },
    {
        name: 'Zoe',
        id: '2',
        email: '[email protected]',
        github: 'zozo'
    },
    {
        name: 'Mark',
        id: '3',
        email: '[email protected]',
        github: 'emman'
    },
    {
        name: 'Joe',
        id: '4',
        email: '[email protected]',
        github: 'joey'
    },
    {
        name: 'Seb',
        id: '5',
        email: '[email protected]',
        school: 'UWA'
    }
]

and to get mark(2) you should use:

team[3].name

1 Comment

He provide console.log output which print class name of objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.