Post by NamasteDev.com
38,114 followers
Want to loop over an object like an array? 🔑 Most developers don't know this exists. The old way of iterating over an object feels clunky every time: ❌ Verbose const user = {"{ name: 'Akshay', age: 25, role: 'dev' }"}; Object.keys(user).forEach(key => {"{"} console.log(key, user[key]); {"}"}); ✅ Clean with Object.entries() Object.entries(user).forEach(([key, value]) => {"{"} console.log(key, value); {"}"}); // "name" "Akshay" // "age" 25 // "role" "dev" Object.entries() gives you both the key and value together as a pair, no more reaching back into the object with obj[key]. Works great with all your favourite array methods too: // Filter only string values Object.entries(user) .filter(([_, v]) => typeof v === "string") // Build a summary string Object.entries(user) .map(([k, v]) => `${"{k}"}: ${"{v}"}`) .join(", ") Once you know the trio, Object.keys() for just keys, Object.values() for just values, Object.entries() for both You'll rarely need to manually loop an object again.