Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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 如何记录数组中的每个元素?_Javascript_Arrays - Fatal编程技术网

Javascript 如何记录数组中的每个元素?

Javascript 如何记录数组中的每个元素?,javascript,arrays,Javascript,Arrays,我遇到了与forEach方法相关的问题。我已经尝试了所有我能想到的写这段代码的方法,但是问题一每次都是错误的 function exerciseOne(names){ // Exercise One: In this exercise you will be given and array called names. // Using the forEach method and a callback as it's only argument, console log // each

我遇到了与forEach方法相关的问题。我已经尝试了所有我能想到的写这段代码的方法,但是问题一每次都是错误的

function exerciseOne(names){

// Exercise One: In this exercise you will be given and array called names. 

// Using the forEach method and a callback as it's only argument, console log

// each of the names.
}


// MY CODE: 

function logNames(name){

  console.log(name);
}

 names.forEach(logNames);

在代码中记录整个阵列。对数组使用
forEach
方法并记录元素

您需要将回调传递给
forEach()
回调中的第一个元素将是数组的元素,该元素被认为是其迭代的元素。记录下来

函数练习一(名称){
name.forEach(x=>console.log(x));
}

exerciseOne(['John'、'peter'、'mart'])
只需使用
console.log
作为回调,每次记录第一个参数(当前项):

函数练习一(名称){
name.forEach(name=>console.log(name));
}

练习一([“杰克”、“乔”、“约翰”、“鲍勃”)不要发布外部链接。使用编辑并在此处添加您的代码。此链接已断开(至少对我而言)。链接要求我登录
names.forEach(函数(项){console.log(项);})
我看到您的代码存在的唯一问题是,您没有实现
exerciseOne()
方法中的逻辑。您救了我的命,我永远感激您@如果你对它满意,CODYHYES会考虑接受这个答案。COLOL.LOG是一个你可以做的函数:<代码> No.FooCH(控制台.log)< /Cord> @ FareedAlnamrouti,它不起作用。code>forEach
传递所有将被记录的其他参数。