私が持っている場合
var numbs = [1, 2, 3, 4]
var new numbs = [1 + Math.random() * 2 - 1, 2 + '' , 3 + etc....
だから私は次のようなものになります:
var new numbs = [.877, 2.166, 2.456, 4.235]
これを行うためのより良い方法が必要です...
私が持っている場合
var numbs = [1, 2, 3, 4]
var new numbs = [1 + Math.random() * 2 - 1, 2 + '' , 3 + etc....
だから私は次のようなものになります:
var new numbs = [.877, 2.166, 2.456, 4.235]
これを行うためのより良い方法が必要です...
Math.random() は基本的に、0 から 1 の間のランダムな 10 進数を生成するために使用されます。したがって、整数要素の取得に関心がある場合は、Math.floor(Math.random()) または Math.ceil(Math.random()) を使用します。または必要に応じて Math. round(Math. random()) 。
// This gives you an array with 4 items all collected using Math.random()
var nums = Array.apply(null, Array(4)).map(function(v, key) {
return Math.round(Math.random() * 100) / 100;
});
nums; // [0.64, 0.35, 0.36, 0.44]
key
その後、計算で (インデックス) を使用できます。
// This gives you an array with 4 items all collected using Math.random()
var nums = Array.apply(null, Array(4)).map(function(v, key) {
return key + Math.round(Math.random() * 100) / 100;
});
nums; // [0.36, 1.52, 2.35, 3.89]
var nums = Array.apply(null, Array(4)).map(function(){});
基本的には次のように書くのと同じです:
var nums = [];
for ( var i = 0; i < 4; i++ ) {
nums.push( /*something*/ );
}
しかし、あなたはクローズドスコープを取得します。