Loop Arrays
var food = ["burrito","taco","quesadilla"]
// for
for (var i = 0; i < food.length; i++) {
console.log(food[i])
}
// forEach
food.forEach(function(item, i) {
console.log(item, i);
console.log(food[i])
});
// for of
for (var item of food) {
console.log(item);
}
Loop Objects
var person = { name: "Rob", height: 74, isMale: true };
// for in
for (var key in person) {
console.log(key, person[k]);
if (person.hasOwnProperty("isMale") && obj.isMale) {...}
}
// convert the object to arrays, then loop the array
var keys = Object.keys(person);
console.log(keys); // ["name", "height", "isMale"]
var values = Object.values(person);
console.log(values); // ["Rob", 74, true]
var entries = Object.entries(person);
console.log(entries); // [["name", "Rob"], ["height", 74, ["isMale", true]]
Loop through all first table td
var tables = document.getElementsByTagName("table");
for (var i = 0; i < tables.length; i++) {
var td = tables[i].querySelectorAll('tr > td:first-child');
for (var j = 0; j < td.length; j++) {
console.log(td[j])
}
}