-1

I have the following array and I want to find the second object by only name and email properties using find method. How do I need to specify these properties in the method?

 const data = [
  {
    name: 'bob',
    email: 'asd@mail.com',
    password: 'sss'
  },
  {
    name: 'bill',
    email: 'www@mail.com',
    password: 'eee'
  },
  {
    name: 'sean',
    email: 'qqq@mail.com',
    password: 'xxx'
  }
]
Darkhan Mukhtar
  • 137
  • 1
  • 7
  • 1
    `data.find(p => p.name === 'bill' && p.email === 'www@mail.com')` (you provide a function that accepts the array element as argument and returns true or false based on whether it matches) There's a pretty clear example at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find –  May 13 '20 at 08:56

1 Answers1

0

In the callback function check if name and email matches

const data = [{
    name: 'bob',
    email: 'asd@mail.com',
    password: 'sss'
  },
  {
    name: 'bill',
    email: 'www@mail.com',
    password: 'eee'
  },
  {
    name: 'sean',
    email: 'qqq@mail.com',
    password: 'xxx'
  }
];

const newData = data.find(item => item.name === 'bill' && item.email === 'www@mail.com');

console.log(newData)
brk
  • 48,835
  • 10
  • 56
  • 78