1

I wrote a function in javascript expression to check if the result is true or false but i am always getting undefined error

var array = [{
    email: '[email protected]',
    password: '123'
  },
  {
    email: '[email protected]',
    password: '123'
  }
];

let main = function(email, password) {

  return array.forEach((row) => {
    if (row.email === email && row.password === password) {
      return true

    } else {
      return false
    }
  });

};

var checkLogin = main('[email protected]', '123');
console.log(checkLogin)

checkLogin always return undefined

2
  • Why are you returning the forEach loop? Commented Dec 10, 2019 at 19:51
  • 1
    array.forEach doesn't return anything. I think you meant to use map Commented Dec 10, 2019 at 19:51

5 Answers 5

4

It's because forEach does not return anything. You can use simple for-loop, like this:

var array = [
{email: '[email protected]', password: '123'},
{email: '[email protected]', password: '123'}
];

let main = function(email, password) {

    for (var i = 0; i < array.length; i++) {
        var row = array[i];
        if (row.email === email && row.password === password) {
            return true
        }
    }
    return false;
};

var checkLogin = main('[email protected]', '123');
console.log(checkLogin)

Also, take a look at some(), includes(), find() and findIndex()

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

1 Comment

btw, this works only with the given array order, but not if the wanted part is the second element of array. you need to move return false to the end of the function, not inside of the for statement, because this ends the iteration with the first element.
2

The forEach array function doesn't return anything. If you touch looped array inside it then you are able to modify existing array without copying it.

1 Comment

It loops over the array. It doesn't necessarily modify anything.
1

there's a problem with foreach. it doesn't return anything

var array = [
{email: '[email protected]', password: '123'},
];

let main = function(email, password) {

   for (var i = 0; i < array.length; i++) {
   if (array[i].email === email && array[i].password === password) {
    return true
     } 
       };
      return false
     };

   var checkLogin = main('[email protected]', '123');
   console.log(checkLogin) // returns true

1 Comment

btw, this works only with the given array order, but not if the wanted part is the second element of array. you need to move return false to the end of the function, not inside of the for statement, because this ends the iteration with the first element.
0

there is something wrong with this logic:

return array.forEach((row) => {
if (row.email === email && row.password === password) {
  return true

} else {
  return false
}
});

without this logic it returns anything you want

1 Comment

forEach doesn't return anything
0

You could take Array#some and return the result of the check.

var array = [{ email: '[email protected]', password: '123' }, { email: '[email protected]', password: '123' }];

let main = (email, password) =>
        array.some(row => row.email === email && row.password === password);

var checkLogin = main('[email protected]', '123');
console.log(checkLogin)

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.