基本数组构造的函数方法 [英] Functional approach to basic array construction
本文介绍了基本数组构造的函数方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,作用于myArray
:
var myArray = [];变量 i;for(i = 0; i <20; i += 1) {myArray.push(Math.random());}
在没有虚拟变量 i
的情况下,是否有与上述功能等效的功能?
最喜欢的答案:
while(myArray.push(Math.random()) <20);
$.map(Array(20), Math.random);
for(var myArray = []; myArray.push(Math.random()) <20;);
解决方案
不是在 ES5 中,没有真正的功能等同于它,因为你必须有一个数量为 20 的东西来应用地图......
>
var my20ElementArray = [0,1,2,3,4,5,6,7,8,9,10];var myArray = my20ElementArray.map(Math.random);
您可以创建一个 xrange-like 函数 Python 中的内容,但这只会隐藏此内容函数内未使用的"变量.
This is my code, acting upon myArray
:
var myArray = [];
var i;
for(i = 0; i < 20; i += 1) {
myArray.push(Math.random());
}
Is there a functional equivalent of the above that does without the dummy variable i
?
Favorite answers:
while(myArray.push(Math.random()) < 20);
$.map(Array(20), Math.random);
for(var myArray = []; myArray.push(Math.random()) < 20;);
解决方案
Not in ES5, there's no real functional equivalent to it, as you have to have something which has an amount of 20 to apply map to...
var my20ElementArray = [0,1,2,3,4,5,6,7,8,9,10];
var myArray = my20ElementArray.map(Math.random);
You could create an xrange-like function what is in Python but that would just hide this "unused" variable inside a function.
这篇关于基本数组构造的函数方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文