loop an array in javascript
let array = ["loop", "this", "array"]; // input array variable
for (let i = 0; i < array.length; i++) { // iteration over input
console.log(array[i]); // logs the elements from the current input
}
loop an array in javascript
let array = ["loop", "this", "array"]; // input array variable
for (let i = 0; i < array.length; i++) { // iteration over input
console.log(array[i]); // logs the elements from the current input
}
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
Copyright © 2021 Codeinu
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