示例:如果我们有array = [1,2,5,10]; 输出应该是[100,50,20,10];因此,在每次迭代中转义迭代的当前元素。
2 回答

白衣染霜花
TA贡献1796条经验 获得超10个赞
您可以首先找到总乘积,然后将其除以每次迭代中的当前元素。
const arr = [1,2,5,10];
const product = arr.reduce((acc,curr)=>acc*curr,1);
const res = arr.map(x => product / x);
console.log(res);

幕布斯6054654
TA贡献1876条经验 获得超7个赞
我的建议
[1,2,5,10].map((x, i, arr) => arr.reduce((acc, y, j) => (i !== j) ? acc * y : acc, 1));
添加回答
举报
0/150
提交
取消