Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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循环_Javascript_Foreach - Fatal编程技术网

Javascript forEach循环

Javascript forEach循环,javascript,foreach,Javascript,Foreach,试图实现此功能。我认为问题在我的考虑范围之内 function forEach(array, callback){ console.log(array, callback); for(var i = 0; i < array.length; i++) { } } // testing your code with console.assert var total = 1; var myArray = [1, 2, 3, 4]; function mul

试图实现此功能。我认为问题在我的考虑范围之内

function forEach(array, callback){    
    console.log(array, callback);
    for(var i = 0; i < array.length; i++) {

    }
}

 // testing your code with console.assert
var total = 1;
var myArray = [1, 2, 3, 4];
function multiplyTotal(a) {
    total *= a;
}
forEach(myArray, multiplyTotal);
// and finally assert; if this fails, the program stops
console.assert(total === 24);
函数forEach(数组,回调){
log(数组、回调);
对于(var i=0;i
函数forEach(数组,回调){
log(数组、回调);
对于(var i=0;i
此外,Javascript已经有了一个内置函数:

myArray.forEach(multiplyTotal);

您实际上没有给您打电话
回调
?你以为会发生什么?添加
回调(数组[i])
for
循环中。或者只使用Javascript中现有的
forEach
函数。您的问题是for循环的主体是空的。您需要执行您希望函数在其中执行的操作。
myArray.forEach(multiplyTotal);