← Back to JavaScript Course | Chapter 2: Functions & Objects | Lesson 7 of 10

JS Object Iterations

Iterating over an object's own properties -- unlike an array's sequential indices -- needs its own set of tools, since a plain for...of loop does not work directly on an object. Object.keys(), Object.values(), Object.entries(), and the for...in loop each offer a different way to walk through an object's properties.
Syntax
javascript
for (const key in object) {
  // object[key]
}

Object.keys(object);
Object.values(object);
Object.entries(object);

Object.keys() से Property नाम पाना

Object.keys(obj) एक असली array लौटाता है जिसमें केवल object की अपनी enumerable property के नाम strings के रूप में होते हैं -- तब उपयोगी जब आपको जानना हो कि object में कौन सी properties हैं, या नामों की उस सूची पर array methods (map, filter) इस्तेमाल करने हों।

Note: यह जानने के लिए कि object में कितनी properties हैं, Object.keys(obj).length का इस्तेमाल एक त्वरित तरीका है, क्योंकि objects की अपनी कोई built-in .length property नहीं होती।
Warning: Object.keys() only returns own properties, not ones inherited from a prototype -- which is usually exactly what you want, but worth knowing explicitly.

उदाहरण: Getting Property Names with Object.keys()

javascript
const user = { name: "Sam", age: 30 };
console.log(Object.keys(user));

Object.values() से Values पाना

Object.values(obj) केवल संबंधित values को असली array के रूप में लौटाता है, उसी क्रम में जिसमें Object.keys() उनके नाम लौटाता -- तब उपयोगी जब property के नाम खुद मायने नहीं रखते, केवल उनमें जो store है वह मायने रखता है।

Note: जब आपको किसी object के data को प्रोसेस या aggregate करना हो (जैसे संख्यात्मक values का योग) और विशिष्ट key नामों की परवाह न हो, तब Object.values() का उपयोग कीजिए।
Warning: Object.values() and Object.keys() always return their results in the same relative order for a given object, which lets you safely pair up a key at index i with its value at index i if needed.

उदाहरण: Getting Values with Object.values()

javascript
const user = { name: "Sam", age: 30 };
console.log(Object.values(user));

Object.entries() से Key-Value जोड़ियाँ पाना

Object.entries(obj) [key, value] जोड़ियों का array लौटाता है -- हर एक दो-item का छोटा array -- जो Object.keys() और Object.values() के फ़ायदों को एक call में मिलाता है, और for...of loop या map() call के भीतर destructuring के साथ खास तौर पर अच्छा काम करता है।

Note: जब iterate करते समय आपको key और value दोनों साथ चाहिए, तब Object.keys() बुलाकर फिर हर value अलग से खोजने की बजाय for...of के साथ Object.entries() का उपयोग कीजिए।
Warning: Object.entries() returns a genuinely new array of pairs each time it is called -- modifying the returned array does not affect the original object.

उदाहरण: Getting Key-Value Pairs with Object.entries()

javascript
const user = { name: "Sam", age: 30 };
console.log(Object.entries(user));

for...in से Loop करना

for...in बिना बीच के array के सीधे object की enumerable property नामों पर loop करता है, भावना में array पर for...of जैसा -- Object.keys()/entries() से थोड़ा पुराना पैटर्न, लेकिन फिर भी आमतौर पर दिखता है और कभी-कभी साधारण loop के लिए ज़्यादा सुविधाजनक होता है।

Note: अगर object में विरासत में मिली enumerable properties हो सकती हैं जिन्हें आप विशेष रूप से बाहर रखना चाहते हैं, तो for...in loop को obj.hasOwnProperty(key) से सुरक्षित कीजिए।
Warning: for...in can technically include inherited enumerable properties from the prototype chain, unlike Object.keys()/values()/entries(), which only ever return own properties -- this distinction rarely matters for plain object literals, but is worth knowing.

उदाहरण: Looping with for...in

javascript
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
const user = { name: "Sam", age: 30 };
// Loop over the enumerable keys of `user`, binding each to `key`
// Loop over the enumerable keys of `user`, binding each to `key`
for (let key in user) {
  // Print `key, user[key]` to the console
  // Print `key, user[key]` to the console
  console.log(key, user[key]);
}

सही Iteration Method चुनना

आधुनिक code में आमतौर पर Object.keys()/values()/entries() को तरजीह दी जाती है, क्योंकि वे असली arrays लौटाते हैं जो array methods के साथ स्वाभाविक रूप से काम करते हैं -- for...in वैध बना रहता है, खासकर सीधे सरल loop के लिए, पर array लौटाने वाले methods नतीजों को filter, map या sort करने के लिए ज़्यादा लचीलापन देते हैं।

Note: नए code में ज़्यादातर object iteration ज़रूरतों के लिए for...of (या chained array method) के साथ Object.entries() को डिफ़ॉल्ट बनाइए, for...in पर मुख्यतः पुराने code के साथ काम करते या उसे संभालते समय जाइए।
Warning: Mixing multiple iteration styles inconsistently across a codebase for the same kind of task can make the code harder to follow, even though all the approaches are individually valid.

उदाहरण: Choosing the Right Iteration Method

javascript
const user = { name: "Sam", age: 30 };
console.log(Object.entries(user).map(([key, value]) => `${key}: ${value}`));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.