0

I have the following array in my javascript code:

const users = [
  {
    id: 1,
    email: '[email protected]',
    password: 'password',
    access_token: 'test_user_access_token'
  },

  {
    id: 2,
    email: '[email protected]',
    password: 'password',
    access_token: 'second_user_access_token'
  }
]

From this collection I want to retrieve user by email. So for example I will write:

my_function("[email protected]") it will return this one user. How can I do this?

3 Answers 3

3

You can use Array#find function. Pass a predicate into the function, which will return the first matched item based on that predicate.

const users = [
  {
    id: 1,
    email: '[email protected]',
    password: 'password',
    access_token: 'test_user_access_token'
  },

  {
    id: 2,
    email: '[email protected]',
    password: 'password',
    access_token: 'second_user_access_token'
  }
]

function findByEmail(email) {
  return users.find(x => x.email === email);
}

console.log(findByEmail('[email protected]'));

Sign up to request clarification or add additional context in comments.

Comments

2

That's what the .find() method is for.

const users = [
  {
    id: 1,
    email: '[email protected]',
    password: 'password',
    access_token: 'test_user_access_token'
  },
  {
    id: 2,
    email: '[email protected]',
    password: 'password',
    access_token: 'second_user_access_token'
  }
];

console.log(users.find(u => u.email == '[email protected]'));

So .find() is called on the array, and receives a callback function. The callback will be invoked for each item in the array, for which you return the result of comparing the .email property to the email you're looking for.

As soon as your callback returns a true (or truthy) result, the iteration halts, and returns that object from .find(). If no is found, .find() returns undefined.


Note that this uses arrow function syntax. If you prefer, you can use traditional functions.

console.log(users.find(function(u) { return u.email == '[email protected]' }));

Comments

1

There's always the good old fashioned for-loop:

const users = [{
    id: 1,
    email: '[email protected]',
    password: 'password',
    access_token: 'test_user_access_token'
  },

  {
    id: 2,
    email: '[email protected]',
    password: 'password',
    access_token: 'second_user_access_token'
  }
]

function findUserByEmail(userList, desiredEmailAddress) {
  for (let i = 0; i < userList.length; i++) {
    var user = userList[i];
    if (user.email === desiredEmailAddress) {
      return user;
    }
  }

  return null;
}

var desiredUser = findUserByEmail(users, '[email protected]');
if (desiredUser) {
  console.log('User found by email:\n');
  console.log(desiredUser);
} else {
  console.log('No user found with searched email address');
}

Comments

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.