xxxxxxxxxx
const fruits = { apple: 28, orange: 17 }
for(key in fruits){
console.log(key)
}
xxxxxxxxxx
// `for...of` loop
for (const [key, value] of Object.entries(animals)) {
console.log(`${key}: ${value}`);
}
// `forEach()` method
Object.entries(animals).forEach(([key, value]) => {
console.log(`${key}: ${value}`)
});
xxxxxxxxxx
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
xxxxxxxxxx
const obj = { foo: 'bar', baz: 42 };
Object.entries(obj).forEach(([key, value]) => console.log(`${key}: ${value}`)); // "foo: bar", "baz: 42"
xxxxxxxxxx
var p = {
"p1": "value1",
"p2": null,
"p3": "value3"
};
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
xxxxxxxxxx
const population = {
male: 4,
female: 93,
others: 10
};
let genders = Object.keys(population);
console.log(genders); // ["male","female","others"]
xxxxxxxxxx
const person = {
name: "John",
age: 30,
favoriteColors: ["Red", "Green"]
};
const keys = Object.keys(person);
// Output: ["name", "age", "favoriteColors"]
// 1. Using a for() loop
for (let i = 0; i < keys.length; i++) {
console.log(keys[i]);
}
// Output: "name", "age", "favoriteColors"
xxxxxxxxxx
// ithe agr value likhya ta values print huni agr key likhea ta keys
for (const value of Object.values(obj)) { }
for (const key of Object.keys(obj)) { }
xxxxxxxxxx
const person = {
first_name: 'Monica',
last_name: 'Geller',
phone: '915-996-9739',
email: 'monica37@gmail.com',
street: '495 Grove Street',
city: 'New York',
country: 'USA',
};
const keys = Object.keys(person);
console.log(keys);
// ['first_name', 'last_name', 'phone', 'email', 'street', 'city', 'country'];