Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用forEach向可被3整除的数组值添加100_Javascript_Arrays_Foreach - Fatal编程技术网

Javascript 使用forEach向可被3整除的数组值添加100

Javascript 使用forEach向可被3整除的数组值添加100,javascript,arrays,foreach,Javascript,Arrays,Foreach,我试图将100加到一个可以被3整除的数组中。有人能告诉我我错过了什么吗 var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4, 19, 300, 3775, 299, 36, 209, 148, 169, 299, 6, 109, 20, 58, 139, 59, 3, 1, 139 ]) 在您缺少移动索引的第一个位置,它总是0 test.forEach(function(i, ind){ if(i % 3 === 0){ t

我试图将100加到一个可以被3整除的数组中。有人能告诉我我错过了什么吗

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139
])


在您缺少移动索引的第一个位置,它总是0

test.forEach(function(i, ind){
  if(i % 3 === 0){
    test.splice(ind, 1, i+100);           
  }
});
第二,使用这个问题不是很有效,更容易

test = test.map(i => i % 3 == 0 ? i + 100 : i)
或者如果你想换个地方的话

test.forEach(function(i, ind){
  if(i % 3 === 0) test(ind) = i+100;           
});

在标题的第三位,您写了可被100整除的代码,但在代码i%3中,请更正

如果您的描述是100,为什么是i%3?谢谢。这是一个特别需要forEach而不是map的赋值。我试试看。非常感谢。
test.forEach(function(i, ind){
  if(i % 3 === 0) test(ind) = i+100;           
});