javascript 如何从数组中的每个数字中减去 1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33060790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:04:43 来源:igfitidea点击:
How do I subtract 1 from each number in an array?
提问by W30
If I have
如果我有
var numberarr = [1, 2, 3, 4, 5];
How would i make it into
我将如何使它成为
var numberarr2 = [0, 1, 2, 3, 4];
by decrementing 1 from each element?
通过从每个元素递减 1?
回答by Austin Brunkhorst
回答by Heman Gandhi
Try this:
试试这个:
// Create an array to hold our new values
var numberArr2 = [];
// Iterate through each element in the original array
for(var i = 0; i < numberArr1.length; i++) {
// Decrement the value of the original array and push it to the new one
numberArr2.push(numberArr1[i] - 1);
}