for i in array code example
Example 1: loop an object in javascript
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
Example 2: object for loop
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
Example 3: js object for each
const obj = {
a: 1,
b: 2,
c: 3
};
for (let key in obj) {
console.log(key + " = " + obj[key]);
}
Example 4: for item loop
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}