最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Map function not returning a value - Stack Overflow

programmeradmin2浏览0评论

I'm trying to return a value inside a map function before reaching the default return value at the end of the function.

I noticed the map function is not returning a value for the validateSequence function, but a simple for loop can.

function validateSequence(sequence) {

  const modifers = ['-', 'm', 'b', 'i'];

  sequence.map((seq) => {
    if(!modifers.includes(seq)) {
      return false; // want to return false
    };
  });

  return true;
};


validateSequence(['Z','e','p']); // returns true
function validateSequence(sequence) {

  const modifers = ['-', 'm', 'b', 'i'];

  for(let i=0; i<sequence.length; i++) {
    if(!modifers.includes(sequence[i])) {
      return false;
    };
  };

  return true;
};


validateSequence(['Z','e','p']); // returns false

I expect the map function to return false before it reaches the default return value, true. I know that the map function is executed before reaching the default return value true, so why isn't it returning false?

I'm trying to return a value inside a map function before reaching the default return value at the end of the function.

I noticed the map function is not returning a value for the validateSequence function, but a simple for loop can.

function validateSequence(sequence) {

  const modifers = ['-', 'm', 'b', 'i'];

  sequence.map((seq) => {
    if(!modifers.includes(seq)) {
      return false; // want to return false
    };
  });

  return true;
};


validateSequence(['Z','e','p']); // returns true
function validateSequence(sequence) {

  const modifers = ['-', 'm', 'b', 'i'];

  for(let i=0; i<sequence.length; i++) {
    if(!modifers.includes(sequence[i])) {
      return false;
    };
  };

  return true;
};


validateSequence(['Z','e','p']); // returns false

I expect the map function to return false before it reaches the default return value, true. I know that the map function is executed before reaching the default return value true, so why isn't it returning false?

Share Improve this question edited Aug 7, 2020 at 17:40 Jonathan Hall 79.9k19 gold badges159 silver badges203 bronze badges asked May 5, 2019 at 6:58 DavidDavid 3233 gold badges8 silver badges21 bronze badges 2
  • 2 The map function is returning seomething, but you do not do anything with it. – Marco Commented May 5, 2019 at 7:01
  • map iterates over every item in the array and builds a new array of the return values from the callback (in your case, false and undefined. It won't stop early unless the callback throws an error. It's not the right tool for the job; just use the for loop. – jonrsharpe Commented May 5, 2019 at 7:20
Add a ment  | 

6 Answers 6

Reset to default 3

Here:

sequence.map((seq) => {
  if (!modifers.includes(seq)) {
    return false; 
  };
});

You are returning false from the (seq) => {} provided callback and not the validateSequence function itself. You can't break from map or forEach methods. So, a for loop is a better option here.

You could also check if every item in sequence is present in modifers like this:

function validateSequence(sequence) {
  const modifers = ['-', 'm', 'b', 'i'];
  return sequence.every(letter => modifers.includes(letter))
};

console.log(validateSequence(['Z','e','p']))
console.log(validateSequence(['m', 'i']))

There's several problems here:

  1. Modifier is misspelled as modifer.
  2. The two examples you posted are not the same. In the map() version, that callback you're passing in is being used to return a new value which is going to be in the return of the map() function. When you return false in that callback, you're not returning out of validateSequence(), you're returning out of the callback (which puts false in the return result of map).

You can see this below:

function validateSequence(sequence) {

  const modifers = ['-', 'm', 'b', 'i'];

  const test = sequence.map((seq) => {
    if(!modifers.includes(seq)) {
      return false; // want to return false
    };
  });

  console.log(test); // [false, false, false], this is where your return false is going.

  return true;
};

Map is returning false, for each element in your sequence variable. So if you return that, you'd end up with an array of false elements:

function validateSequence(sequence) {
  const modifers = ['-', 'm', 'b', 'i'];
  return sequence.map((seq) => {
    if(!modifers.includes(seq)) {
      return false; // want to return false
    };
  });
};

Calling validateSequence(['Z','e','p']) will then result in [false, false, false]

Of course, if you store the result of map in a variable, you could the evaluate that, to determine if all, some, any are false if you so like. That is up to you.

.map creates a new array with the results of calling a provided function on every element in the calling array. It isn't a substitute for a loop.

What you are actually doing is creating an array of booleans, but not storing it regardless. So it then finishes that task, then returns true at the bottom.

Try using .forEach, if you wish to iterate over the array or perhaps using .every, which will simplify your logic:

function validateSequence(sequence) {
  const modifers = ['-', 'm', 'b', 'i'];
  return sequence.every(letter => modifers.includes(letter))
};

In your validateSequence function at the end you have return true that's why you always get true. If you want to return result of map then add return to it.

return sequence.map((seq) => {
    if(!modifers.includes(seq)) {
      return false; // want to return false
    } else {
      return true;
    }  
 });

I'am not sure what you want the result, but if you want to break the loop if certain condition passes, then this will not serve your purpose because map always returns a value, it a one-to-one mapping. If you want to break a loop, you simple for loop.

You can do this

function validateSequence(sequence) {

    const modifers = ['-', 'm', 'b', 'i'];

    const isMatch = sequence.some(seq => {
        return modifers.includes(seq)
    })

    return isMatch
}
console.log(validateSequence(['Z', 'e', 'p']))
发布评论

评论列表(0)

  1. 暂无评论