javascript iterate array for in code example
Example 1: js loop through object
const obj = { a: 1, b: 2 };
Object.keys(obj).forEach(key => {
console.log("key: ", key);
console.log("Value: ", obj[key]);
} );
Example 2: javascript loop through array
let my_array = [1, 2, 3, 4, 5];
// standard for loop
for(let i = 0; i < my_array.length; i++) {
console.log(my_array[i]) // 1 2 3 4 5 6
}
// for and of method
for(let i of my_array) {
console.log(i)
}
/*
Results:
1 2 3 4 5
1 2 3 4 5
(From both methods)
*/