简体   繁体   English

map function 返回 1 boolean 值,而不是 Z84E2C64F38F78BA3EA5C905DA27A 值的数组

[英]map function to return 1 boolean value, instead of an array of boolean values

Say you have an array like this:假设你有一个这样的数组:

arrayExample = [1, 2, 3, 4, 5, 6, 7]

I want to use the map function to iterate through arrayExample and return true if all the numbers are less than 8, false if they are not.我想使用 map function 遍历 arrayExample 并在所有数字都小于 8 时返回true ,否则返回false However, when I do this I get an array of trues (like: [true, true, true... etc])但是,当我这样做时,我会得到一组真值(例如:[true, true, true... etc])

Would I be able to return just 1 value?我可以只返回 1 个值吗?

Here is my code so far:到目前为止,这是我的代码:

 var testBoolean = true; var array = [1,2,3,4,5,6,7]; testBoolean = array.map(m => { //if a number in the array is >=8, changes the testBoolean to false and return false only if(m >= 8) { return false; } //VS code said I had to have a return here, I believe its because I need to have in case m < 8 (edited after reading a comment) return true; }) //prints an array [true,true,true,true,true,true,true] document.write(testBoolean);

I'm a bit new to "map" but I believe it does this since it returns a value for every element, just confused on how to make it so it returns 1 true or false .我对“map”有点陌生,但我相信它会这样做,因为它为每个元素返回一个值,只是对如何制作它感到困惑,所以它返回 1 truefalse

For something like this .map() isn't the right tool.对于这样的事情.map()不是正确的工具。 .map() is for converting all the elements in your array to new elements (ie: mapping each element to a new transformed values). .map()用于将数组中的所有元素转换为新元素(即:将每个元素映射到新的转换值)。 As a result, .map() will always return an array (unless this behaviour is intentionally modified).因此, .map()将始终返回一个数组(除非有意修改此行为)。 Instead, you can use .every() for this, which will check if all elements in your array match your condition (ie: if your function returns true for every element, then you'll get true as your result, otherwise you'll get false).相反,您可以为此使用.every() ,它将检查数组中的所有元素是否符合您的条件(即:如果您的 function 为每个元素返回 true,那么您的结果将是 true,否则您将得到错误)。 The .every() method will terminate early if it finds one element which your callback function returns false for, which can help with efficiency:如果.every()方法找到一个您的回调 function 为其返回 false 的元素,它将提前终止,这有助于提高效率:

 const array = [1, 2, 3, 4, 5, 6, 7]; const testBoolean = array.every(m => { if (m >= 8) { return false; } return true; }); console.log(testBoolean);

This can be written more concisely, by just returning the result of m < 8 (this will either evaluate to true or false )这可以写得更简洁,只返回m < 8的结果(这将评估为truefalse

 const array = [1,2,3,4,5,6,7]; const testBoolean = array.every(m => m < 8); console.log(testBoolean);

The simplest solution is to use .some() .最简单的解决方案是使用.some()

We don't need to check every value.我们不需要检查每个值。 We need to find the first value not less than 8.我们需要找到不小于 8 的第一个值。

 const array = [1, 2, 3, 4, 5, 6, 7] const testBoolean =.array.some(m => m >= 8) console.log(testBoolean)

From a more general perspective, if you want to return a single value from an Array, .reduce() is your friend.从更一般的角度来看,如果您想从 Array 返回单个值, .reduce()是您的朋友。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Specifically in your case, you could do特别是在你的情况下,你可以做

array.reduce(function(accumulator, currentVal, i, arr) {
  return accumulator && currentVal < 8;
}, true));

So .reduce() iterates through your array with an initial value true and returns the "previous" value (accumulator) AND whether the current value is less than 8.因此.reduce()使用初始值true遍历您的数组并返回“先前”值(累加器)以及当前值是否小于 8。

You could think of it as你可以把它想象成

(true && (a[0] < 8 && (a[1] < 8... )))

The returned value at each iteration becomes the accumulator of the next.每次迭代的返回值成为下一次的accumulator In this way you could not only do math operations but also change an array of a certain shape (eg array of {w:0, h:0} objects) to an output of another (eg a single number being the sum of all the hypotenuses calculated from each w and h ).这样,您不仅可以进行数学运算,还可以将某个形状的数组(例如{w:0, h:0}对象的数组)更改为另一个的 output(例如,单个数字是所有从每个wh计算的斜边)。

const fn = (z)=>{
  if(z>=8){
    return true;
  }else{
    return false;
   }
}

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const mappedArray = array.map(fn);
 
//for value

const filterdArray = array.filter(fn)

console.log(mappedArray)

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

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