简体   繁体   English

如何使用 for of loop javascript 返回数组中的元素数?

[英]How do I return the number of elements in an array using for of loop javascript?

How do I return the number of elements in an array using for of loop javascript?如何使用 for of loop javascript 返回数组中的元素数?

I know how I can use a for loop to do this but I was wondering if there is a way to do this with a for of loop.我知道如何使用for loop来执行此操作,但我想知道是否有一种方法可以使用for of循环来执行此操作。 In this case would using a regular for loop be better and or the only way to do this is with a loop?在这种情况下,使用常规for loop会更好,或者唯一的方法是使用循环?

Regular for loop that prints the number of elements in the array:打印数组中元素数量的常规 for 循环:

 const arr = [1, 2, 3, 4]; function logElementForLoop(arr) { console.log("For Loop"); let elemInArr = 0; for (let i = 0; i < arr.length; i++) { elemInArr = i + 1; } console.log(elemInArr); } logElementForLoop(arr);

I was wondering if there is a way to do this with a for of loop...我想知道是否有办法用 for of 循环来做到这一点......

Yes, it is possible, simply increment the variable ( elemInArr ) inside the loop:是的,有可能,只需在循环内增加变量( elemInArr ):

 const arr = [1, 2, 3, 4]; function logElementForLoop(arr) { console.log("For Loop"); let elemInArr = 0; for (let i of arr) { elemInArr++; } console.log(elemInArr); } logElementForLoop(arr);

I guess you are looking for something like for of or for in loops我猜你正在寻找类似for offor in循环的东西

let count = 0
for(const item of array) {
  count ++
}
console.log('array contains', count, 'elements')

or或者

let count = 0
for(const index in array) {
  count ++
}
console.log('array contains', count, 'elements')

You need to add 1 to the count, instead of assigning the index plus one.您需要将计数加1 ,而不是分配索引加 1。

 function logElementForLoop(arr) { console.log("For Loop"); let elemInArr = 0; for (let i = 0; i < arr.length; i++) { // add one for each element // elemInArr = elemInArr + 1; // is doing it the long way // elemInArr += 1; // jsut add one, handy for adding different values than one elemInArr++; // standard approach } console.log(elemInArr); } const arr = [1, 2, 3, 4]; logElementForLoop(arr);

Well, if you need to count array elements in for loop..?好吧,如果您需要在 for 循环中计算数组元素..? then然后

 const arr = [1, 2, 3, 4, 5]; // Counter var let counter = 0; // Count in for loop for(let i = 0; i < arr.length; i++) counter++; // Log console.log(counter);

Using a for of loop使用 for of 循环

 const arr = [1, 2, 3, 4]; function logElementForLoop(arr) { console.log("For Loop"); let elemInArr = 0; for (let i of arr ) { elemInArr = arr.indexOf(i) + 1; } console.log(elemInArr); } logElementForLoop(arr);

or simply increment elemInArr或简单地增加elemInArr

 const arr = [1, 2, 3, 4]; function logElementForLoop(arr) { console.log("For Loop"); let elemInArr = 0; for (let i of arr ) { elemInArr++ } console.log(elemInArr); } logElementForLoop(arr)

暂无
暂无

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

相关问题 如何使用javascript中的for循环成对检索数组元素? - How do I retrieve array elements in pairs using the for loop in javascript? 如何使用 for 循环将 javascript 数组的元素打印到网页? - How do I print the elements of a javascript array to the webpage using a for loop? 如何返回不在Javascript映射中的元素数组? - How do I return an array of elements NOT in a Javascript map? 如何找出JavaScript阵列中的元素数量? - How do I find out the number of elements in a Javascript array? 如何使用数字数组使用Java中的reduce函数返回最高和最低数字 - How do I take an array of numbers and return both the highest and the lowest number using the reduce function in Javascript 如何使用 for each 循环根据使用 JavaScript 的给定回调计算数组中的出现次数? - How do I use a for each loop to count the number of occurences in an array based on a given callback using JavaScript? 如何使用JavaScript返回元素内子元素? - How do I return an elements innerchild using JavaScript? 如何使用 javascript 中的提示向数组添加新元素? - How do I add new elements to an array using a promt in javascript? 如何循环遍历元素并使用索引号来显示相对数组对象属性? - How do I loop through elements and use the index number to present the relative array object properties? 我尝试编写一个函数,该函数将返回等于数组中传递的数字的所有数组元素的总和。 我该怎么做? - I try to write a function that will return the sum of all array elements that are equal to the number passed in the array. How can I do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM