📜  乘法数组 javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:27.300000             🧑  作者: Mango

代码示例2
The cause is already known. Here's an alternative - using Array.reduce for your method:

console.log( [1, 2, 3].reduce( (a, b) => a * b ) );
console.log( Array.from( {length: 20} )
  .map( (v, i) => i + 1 )
  .reduce( (a,b) => a * b )
  .toLocaleString());

// for empty arrays, use some initial value
const arr = [];
if (arr.reduce( (a, b) => a * b, -1 ) === -1) {
  console.error(`The given array ${arr} is empty`);
}