简体   繁体   English

JavaScript Map函数未返回值

[英]JavaScript Map function not returning a value

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. 我注意到map函数没有为validateSequence函数返回值,但是一个简单的for循环可以。

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. 我希望map函数在达到默认返回值true之前返回false。 I know that the map function is executed before reaching the default return value true, so why isn't it returning false? 我知道map函数是在达到默认返回值true之前执行的,为什么它不返回false?

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. 您从提供的回调(seq) => {}返回了false ,而不是validateSequence函数本身返回了false You can't break from map or forEach methods. 你不能breakmapforEach方法。 So, a for loop is a better option here. 因此,在这里for循环是更好的选择。

You could also check if every item in sequence is present in modifers like this: 您还可以检查sequence every项目是否every存在于modifers如下所示:

 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'])) 

Map is returning false, for each element in your sequence variable. 对于sequence变量中的每个元素,Map返回false。 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] 调用validateSequence(['Z','e','p'])将导致[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. 当然,如果将map的结果存储在一个变量中,则可以对它进行求值,以确定是否愿意,如果全部,某些或任何一个为假。 That is up to you. 那取决于你。

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. map()版本中,您传递的回调用于返回新值,该值将在map()函数的返回值中。 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 ). 当您在该回调中返回false时,您并没有返回validateSequence() ,而是从回调中返回了(这将false放入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 creates a new array with the results of calling a provided function on every element in the calling array. .map创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。 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. 这样就完成了该任务,然后在底部returns true

Try using .forEach , if you wish to iterate over the array or perhaps using .every , which will simplify your logic: 尝试使用.forEach ,如果你想迭代这个数组或可能使用.every ,这将简化你的逻辑:

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 . 最后,在您的validateSequence函数中,您return true ,这就是为什么您总是得到true的原因。 If you want to return result of map then add return to it. 如果要返回map结果,请在其中添加return

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. 我不确定您想要的结果是什么,但是如果您希望在某些条件通过时中断循环,那么这将无法满足您的目的,因为map始终返回一个值,即一对一的映射。 If you want to break a loop, you simple for loop. 如果要中断循环,可以简单地for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM