var array = [1, 2, 3, 4, 5];
// made an array
for (var i = 0; array[i]; i++) // browse your array with this simple condition on index
console.log('element '+i+' = '+array[i]);
//output:
element 0 = 1
element 1 = 2
element 2 = 3
element 3 = 4
element 4 = 5
Posted by: Guest
on August-04-2021
0
how to cycle through an array js
// Requiring the lodash library
// src: https://lodash.com/docs/4.17.15#forEach
// console: npm i --save lodash
const _ = require("lodash")
// Use of _.forEach() method
_.forEach([1, 2], function(value) {
console.log(value)
});
// => Logs `1` then `2`.
// traversing an object
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
console.log(key)
})
// => Logs 'a' then 'b' (iteration order is not guaranteed).
//////////////// To not use lodash ////////////////
// src: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element)
}
[2, 5, 9].forEach(logArrayElements)
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email