const obj = {
a: 1,
b: 2,
c: 3,
d: 4
}
for (const key in obj) {
console.log( obj[key] )
}
// Result: 1, 2, 3, 4
for (key in object) {
// body of for...in
}
// for... in
for (let i in str) {
console.log(str[i]);
}
#list comprehension: squaring numbers in a list a list.
cardinals = [1,2,3,4,5]
squares = [i**2 for i in cardinals]
print(squares)
for (key in object) {
// body of for...in
}
//basic for in loop
const car = {
carName: "Ford",
carDoor: 4,
carSpeed: 180
}
for (objKey in car) {
console.log(car[objKey])
}
//output
Ford
4
180
let list = ["a", "b", "c"];
// for in
for (let i in list) {
// i is index
console.log(i); // "0", "1", "2",
console.log(list[i]); // "a", "b", "c"
}
// for of
for (let i of list) {
// i is value
console.log(i); // "a", "b", "c"
}
const obj = {
a: 1,
b: 2,
c: 3,
};
// Long-hand
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = obj[key];
// ...
}
// Short-hand
for (const key in obj) {
const value = obj[key];
// ...
}