Home > Net >  How to access an object from among the objects of an array?
How to access an object from among the objects of an array?

Time:07-02

How to access an object from among the objects of an array?

like this :

const users = [
  { id: 1, name: "John" },
  { id: 2, name: "David" },
  { id: 3, name: "Marie" },
];

How to access the object by name or ...?

CodePudding user response:

you can use like this:

 const john = users.find(item => item.name === 'John');
    
    alert(john.id); // 1

CodePudding user response:

users[array index].id or .name

Example : users[0].name (this gives you John)

CodePudding user response:

  const users = [
  { id: 1, name: "John" },
  { id: 2, name: "David" },
  { id: 3, name: "Marie" },
  ];

  let test = users.find(user => user.name="jugurtha")
  • Related