在这种情况下,如何返回一个最大值设置为大于5的数组?
arr.forEach(function(index, theArray) {
if (theArray[index] > 5) theArray[index] = 5
})
console.log(arr) // [5.5, 0.1 8.4, 4.3, etc]
想 [5, 0.1, 5, 4.3, etc]
您可以使用map
和Math.min
:
arr = arr.map(n => Math.min(n, 5));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句