假设 x = 120,并且您有一个数组:[1,2,3,4,5]。
我想创建一个数组,其结果是 120 除以数组中的每个数字,因此它会产生
[120、60、40、30、24]?
最佳答案
您可以使用Array.prototype.map
使用 ES5:
var dividedBy120 = [1,2,3,4,5].map(function(currentValue, index, array){
return 120/currentValue;
});
// dividedBy120 = [120, 60, 40, 30, 24]
使用 ES6 箭头函数:
var dividedBy120 = [1,2,3,4,5].map(currentValue => 120/currentValue);
// dividedBy120 = [120, 60, 40, 30, 24]
关于javascript - 如何将一个数字除以数组中的每个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927398/