将函数映射到 return 1 个布尔值,而不是布尔值数组
map function to return 1 boolean value, instead of an array of boolean values
假设你有这样一个数组:
arrayExample = [1, 2, 3, 4, 5, 6, 7]
我想用map函数遍历arrayExample和return true如果所有数字都小于8,false 如果他们不是。但是,当我这样做时,我得到了一组真值(例如:[true, true, true... 等])
我可以 return 仅 1 个值吗?
到目前为止,这是我的代码:
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);
我对“地图”有点陌生,但我相信它能做到这一点,因为它 return 是每个元素的一个值,只是对如何制作它感到困惑所以它 return 1 true 或 false.
对于这样的事情 .map()
不是正确的工具。 .map()
用于将数组中的所有元素转换为新元素(即:将每个元素映射到新的转换值)。因此,.map()
将始终 return an array (unless this behaviour is intentionally modified). Instead, you can use .every()
,这将检查数组中的 所有 元素是否符合您的条件(即:如果您的函数 returns 对每个元素都为真,则结果为真,否则为假)。如果 .every()
方法发现一个元素的回调函数 returns 为 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);
这可以写得更简洁,只需返回 m < 8
的结果(这将计算为 true
或 false
)
const array = [1,2,3,4,5,6,7];
const testBoolean = array.every(m => m < 8);
console.log(testBoolean);
最简单的解决方案是使用 .some()
。
我们不需要检查每个值。我们需要找到第一个不小于8的值。
const array = [1, 2, 3, 4, 5, 6, 7]
const testBoolean = !array.some(m => m >= 8)
console.log(testBoolean)
从更一般的角度来看,如果您想 return 数组中的单个值,.reduce()
是您的朋友。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
具体到你的情况,你可以这样做
array.reduce(function(accumulator, currentVal, i, arr) {
return accumulator && currentVal < 8;
}, true));
因此 .reduce()
使用初始值 true
和 return 遍历您的数组“前一个”值(累加器)以及当前值是否小于 8。
你可以把它想象成
(true && (a[0] < 8 && (a[1] < 8 ... )))
每次迭代的 returned 值成为下一次的 accumulator
。通过这种方式,您不仅可以进行数学运算,还可以将特定形状的数组(例如 {w:0, h:0}
对象的数组)更改为另一个的输出(例如,单个数字是从每个计算的所有斜边的总和w
和 h
).
假设你有这样一个数组:
arrayExample = [1, 2, 3, 4, 5, 6, 7]
我想用map函数遍历arrayExample和return true如果所有数字都小于8,false 如果他们不是。但是,当我这样做时,我得到了一组真值(例如:[true, true, true... 等])
我可以 return 仅 1 个值吗?
到目前为止,这是我的代码:
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);
我对“地图”有点陌生,但我相信它能做到这一点,因为它 return 是每个元素的一个值,只是对如何制作它感到困惑所以它 return 1 true 或 false.
对于这样的事情 .map()
不是正确的工具。 .map()
用于将数组中的所有元素转换为新元素(即:将每个元素映射到新的转换值)。因此,.map()
将始终 return an array (unless this behaviour is intentionally modified). Instead, you can use .every()
,这将检查数组中的 所有 元素是否符合您的条件(即:如果您的函数 returns 对每个元素都为真,则结果为真,否则为假)。如果 .every()
方法发现一个元素的回调函数 returns 为 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);
这可以写得更简洁,只需返回 m < 8
的结果(这将计算为 true
或 false
)
const array = [1,2,3,4,5,6,7];
const testBoolean = array.every(m => m < 8);
console.log(testBoolean);
最简单的解决方案是使用 .some()
。
我们不需要检查每个值。我们需要找到第一个不小于8的值。
const array = [1, 2, 3, 4, 5, 6, 7]
const testBoolean = !array.some(m => m >= 8)
console.log(testBoolean)
从更一般的角度来看,如果您想 return 数组中的单个值,.reduce()
是您的朋友。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
具体到你的情况,你可以这样做
array.reduce(function(accumulator, currentVal, i, arr) {
return accumulator && currentVal < 8;
}, true));
因此 .reduce()
使用初始值 true
和 return 遍历您的数组“前一个”值(累加器)以及当前值是否小于 8。
你可以把它想象成
(true && (a[0] < 8 && (a[1] < 8 ... )))
每次迭代的 returned 值成为下一次的 accumulator
。通过这种方式,您不仅可以进行数学运算,还可以将特定形状的数组(例如 {w:0, h:0}
对象的数组)更改为另一个的输出(例如,单个数字是从每个计算的所有斜边的总和w
和 h
).