loop through a list js code example
Example 1: javascript loop through object example
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
Example 2: javascript code to loop through array
var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 3: loop through map in js
const object = {'a': 1, 'b': 2, 'c' : 3};
for (const [key, value] of Object.entries(object)) {
console.log(key, value);
}
Example 4: how to cycle through an array js
const _ = require("lodash")
_.forEach([1, 2], function(value) {
console.log(value)
});
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
console.log(key)
})
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element)
}
[2, 5, 9].forEach(logArrayElements)