[英]add 8 to my randomly generated array
As the title suggests, I want to add 8 to my randomly generated array. 如标题所示,我想在随机生成的数组中添加8。 For example if the array produces the following: [8,64,92,3,65,23,76,92].
例如,如果数组产生以下内容:[8,64,92,3,65,23,76,92]。 Underneath this array I want the following to appear [16,72,100,11..etc].
我希望在此数组下面显示以下内容:[16,72,100,11..etc]。
How would I go about doing this. 我将如何去做。 I tried
var newarray = arr +8
but this just added 8 to the last value in the array. 我尝试了
var newarray = arr +8
但这只是将8添加到数组的最后一个值。
var arr = []
function array() {
while(arr.length < 8){
var randomnumber = Math.ceil(Math.random()*100)
if(arr.indexOf(randomnumber) > -1) continue;
arr[arr.length] = randomnumber;
}
document.getElementById('Output').innerHTML = "Array values before the update:" + "<br>" + arr;
}
I suggest make use of Array.prototype.map 我建议利用Array.prototype.map
You can use arrow function: 您可以使用箭头功能:
function add(arr){
return arr.map(a => a+8));
}
or do this without es6: 或在没有es6的情况下执行此操作:
function add(arr){
return arr.map(function(a){
return a+8;
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.