【发布时间】:2021-11-16 05:46:02
【问题描述】:
我正在为一些逻辑而苦苦挣扎,但我认为我已经接近了。有没有办法从布尔数组中获取真值的数量?
const checkedState = [true, false, true]
function handleCourseProgress() {
//for each value that is true in the array...
checkedState.forEach((value) => {
//if the value is true...
if (value === true) {
//count and total them up here....
setCourseProgress(//return the count//);
}
});
}
【问题讨论】:
-
Array.reduce() 可能是最简单的方法。
-
这能回答你的问题吗? Remove all falsy values from an array - 只计算过滤后剩下的内容。
-
当然可以!不知道用什么方法,谢谢
标签: javascript arrays reactjs foreach boolean