简体   繁体   English

如何使用for循环控制台记录阵列的每个项目?

[英]How to use a for loop to console log each item of an array?

I'm working on an assignment that experiments with different types of loops. 我正在做一个实验,尝试不同类型的循环。 I have to use a for loop to console.log each item in the array cars . 我必须使用for循环来console.log阵列cars中的每个项目。 I can only use a for loop, no other methods such as for each . 我只能使用for循环,而不能使用诸如for each其他方法。 What am I missing? 我想念什么?

I have tried console logging cars but that logs the entire array a number of times equal to the number of strings in the array, which is obviously not correct. 我已经尝试过控制台记录cars但是记录整个数组的次数等于数组中字符串的数量,这显然是不正确的。 I'm also fairly certain that the .length method used in the for loop is incorrect as well. 我也可以肯定, for循环中使用的.length方法也是不正确的。

 const cars = ["ford", "chevrolet", "dodge", "mazda", "fiat"]; for (let i = 0; i < cars.length; i++) { console.log(cars) } 

cars refers to the entire array. cars是指整个阵列。 What you want is to access an item in the array by index in the for loop: that can be done by using cars[i] : 您想要的是通过for循环中的索引访问数组中的一项:可以通过使用cars[i]来完成:

 const cars = ["ford", "chevrolet", "dodge", "mazda", "fiat"]; for (let i = 0; i < cars.length; i++) { console.log(cars[i]); } 

Even better: you an use forEach instead , which is way more readable: 更好的是:您可以使用forEach代替 ,这样可读性更高:

 const cars = ["ford", "chevrolet", "dodge", "mazda", "fiat"]; cars.forEach(car => { console.log(car); }); 

You can use foreach too 您也可以使用foreach

 const cars = ["ford", "chevrolet", "dodge", "mazda", "fiat"]; cars.forEach(function(element, index) { console.log(element); }); 

In the above code, you need to log cars[i] to the console: 在上面的代码中,您需要将cars[i]登录到控制台:

 const cars = ["ford", "chevrolet", "dodge", "mazda", "fiat"]; for (let i = 0; i < cars.length; i++) { console.log(cars[i]) } 

cars is the array, and to access one item in the array, you need to use the numerical index (in this case i ). cars是数组,要访问数组中的一项,您需要使用数字索引(在本例中为i )。

You can also eliminate indexes entirely by using a forEach loop, which is often simpler and faster than a traditional for loop: 您还可以通过使用forEach循环来完全消除索引,该循环通常比传统的for循环更简单,更快捷:

 const cars = ["ford", "chevrolet", "dodge", "mazda", "fiat"]; cars.forEach(car => console.log(car)); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jQuery each 成功 console.log 一个 JSON 数组值 - How to use jQuery each to successfully console.log a JSON array value 如何在Vue的v-for循环中console.log一个项目 - How to console.log an item inside a v-for loop in Vue 如何调用JS数组项/读取控制台日志返回 - How to call JS array item / Read console log return 如何在每次迭代中使用 reduce() 到 console.log? - How to I use reduce() to console.log on each iteration? 如何在功能参数中使用循环并将结果记录到控制台 - how to use a loop within a functions parameter and log the result to the console 如何记录每个数组项的索引? - How can I log the index of each array item? 如何在循环外使用每个循环变量(项目) - How can I use each loop variable (item) outside of loop 如何在循环中或每个数组元素中使用.clone()? - How to use .clone() in a loop or for each array element? 在 javascript 中,如何使用 fs.writeFile 并循环数组并在新文本文件中垂直打印数组中的每个项目 - In javascript, how do I use fs.writeFile and loop an array and completely print each item from the array vertically in a new text file 一个 function,它以一个数组作为参数,带有一个 forEach 循环,它 console.log 每个元素和 function 中每次迭代的每个索引 - A function which takes an array as argument, with a forEach loop which console.log each element and each index for every iteration inside the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM