📘 Lesson  ·  Lesson 30

Object Methods

Object Methods

Object.keys, Object.values, Object.entries

let student = { name: "Aman", rollNo: 12, marks: 85 };

Object.keys(student);     // ["name", "rollNo", "marks"]     — the keys
Object.values(student);   // ["Aman", 12, 85]                — the values
Object.entries(student);  // [["name","Aman"], ["rollNo",12], ["marks",85]]

Object.keys(student).length;   // 3 — how many properties?
These three built-in methods convert an object into an ARRAY, which lets you use all the array methods you just learned. Object.keys() gives an array of the property names, Object.values() gives their values, and Object.entries() gives an array of [key, value] pairs. Note that objects have no .length property — to count properties, use Object.keys(obj).length. Turning objects into arrays like this is the bridge between the two data structures.

Looping Over Objects

let student = { name: "Aman", marks: 85 };

// for...in — the classic way (gives you each KEY)
for (let key in student) {
    console.log(key, student[key]);     // name Aman, marks 85
}

// Object.entries + for...of — modern and clear
for (let [key, value] of Object.entries(student)) {
    console.log(key, value);
}

// Or with forEach:
Object.keys(student).forEach(key => console.log(key, student[key]));
Objects can't be looped with a plain for loop (they have no indexes), so you use one of these approaches. for...in is the classic — it hands you each key, and you look up the value with student[key] (bracket notation, because the key is in a variable). The modern alternative converts to an array first with Object.entries(), then uses for...of. Recall the earlier rule: for...of for arrays, for...in for objects. Both work; pick whichever reads better.

Copying and Merging Objects

let original = { name: "Aman", marks: 85 };

let bad = original;              // ✗ NOT a copy — both names point to the SAME object
bad.name = "Priya";
console.log(original.name);      // "Priya" — the original changed!

let copy = { ...original };      // ✓ spread operator — a real copy
let copy2 = Object.assign({}, original);   // ✓ older way

// Merging two objects:
let defaults = { theme: "light", lang: "en" };
let userPrefs = { theme: "dark" };
let settings = { ...defaults, ...userPrefs };   // { theme:"dark", lang:"en" }
Assigning an object to another variable does NOT copy it — both variables refer to the same object in memory. Change one and the "other" changes too, which surprises everyone once. This is the reference behaviour you met when [1,2] === [1,2] was false. To make a genuine copy, use the spread operator {...obj}. Spreading also merges: later objects overwrite earlier ones, which is exactly how you apply user preferences on top of defaults. (Note: this is a "shallow" copy — nested objects inside are still shared.)

Arrays of Objects — the most common real structure

let students = [
    { name: "Aman",  marks: 85 },
    { name: "Priya", marks: 92 },
    { name: "Ravi",  marks: 28 }
];

// Now combine array methods with objects:
let names = students.map(s => s.name);              // ["Aman","Priya","Ravi"]
let passed = students.filter(s => s.marks >= 33);   // the two who passed
let total = students.reduce((sum, s) => sum + s.marks, 0);   // 205
let topper = students.find(s => s.marks > 90);      // the Priya object
An array of objects is THE standard way real data arrives — from databases, from APIs, from forms. A list of students, products, orders, or posts is always shaped like this. And here's the payoff of the last few chapters: all the array methods work beautifully on it. map to pull out one field, filter to select matching records, reduce to total a column, find to locate one record. This combination — arrays of objects plus map/filter/reduce — is the single most important data-handling skill in JavaScript.

Practical Patterns

// Shorthand property names — when key and variable share a name:
let name = "Aman", marks = 85;
let student = { name, marks };        // same as { name: name, marks: marks }

// Sorting an array of objects:
students.sort((a, b) => b.marks - a.marks);   // highest marks first

// Checking a key exists before using it:
if ("marks" in student) { console.log(student.marks); }
Three patterns you'll use constantly. Shorthand properties: if a variable is already named the same as the key, just write it once — { name, marks }. Sorting objects: reuse the compare-function trick from array methods, but compare a property (b.marks - a.marks sorts descending). And checking keys with in before reading them prevents undefined surprises. With objects, arrays, and functions all under your belt, you're now ready for the DOM — where JavaScript starts changing real web pages.

Exam Corner

Q: What does Object.keys(obj) return? An array of the object's property names.

Q: How do you count an object's properties? Object.keys(obj).length — objects have no .length.

Q: Does let b = a copy an object? No — both variables point to the same object. Use {...a} to copy.

Q: How do you merge two objects? { ...obj1, ...obj2 } — later keys overwrite earlier ones.

Q: How do you get all names from an array of student objects? students.map(s => s.name)

Object.keys, Object.values, Object.entries

let student = { name: "Aman", rollNo: 12, marks: 85 };

Object.keys(student);     // ["name", "rollNo", "marks"]     — keys
Object.values(student);   // ["Aman", 12, 85]                — values
Object.entries(student);  // [["name","Aman"], ["rollNo",12], ["marks",85]]

Object.keys(student).length;   // 3 — kitni properties?
ये तीन built-in methods object को ARRAY में बदलते हैं, जो आपको अभी सीखे सारे array methods use करने देता है. Object.keys() property names की array देता है, Object.values() उनकी values, और Object.entries() [key, value] जोड़ों की array. ध्यान दीजिए objects में .length property नहीं होती — properties गिनने को Object.keys(obj).length use कीजिए. Objects को ऐसे arrays में बदलना दोनों data structures के बीच का पुल है.

Objects पर Loop

let student = { name: "Aman", marks: 85 };

// for...in — classic tarika (har KEY deta hai)
for (let key in student) {
    console.log(key, student[key]);     // name Aman, marks 85
}

// Object.entries + for...of — modern aur saaf
for (let [key, value] of Object.entries(student)) {
    console.log(key, value);
}

// Ya forEach ke saath:
Object.keys(student).forEach(key => console.log(key, student[key]));
Objects पर सादे for loop से loop नहीं हो सकता (उनके indexes नहीं), तो आप इनमें से कोई approach use करते हैं. for...in classic है — यह आपको हर key देता है, और आप student[key] से value देखते हैं (bracket notation, क्योंकि key variable में है). आधुनिक विकल्प पहले Object.entries() से array में बदलता है, फिर for...of use करता है. पहले वाला rule याद कीजिए: arrays के लिए for...of, objects के लिए for...in. दोनों चलते हैं; जो बेहतर पढ़े वही चुनिए.

Objects Copy और Merge करना

let original = { name: "Aman", marks: 85 };

let bad = original;              // ✗ copy NAHI — dono naam VAHI object point karte
bad.name = "Priya";
console.log(original.name);      // "Priya" — original badal gaya!

let copy = { ...original };      // ✓ spread operator — asli copy
let copy2 = Object.assign({}, original);   // ✓ purana tarika

// Do objects merge karna:
let defaults = { theme: "light", lang: "en" };
let userPrefs = { theme: "dark" };
let settings = { ...defaults, ...userPrefs };   // { theme:"dark", lang:"en" }
Object को दूसरे variable में assign करना उसे COPY नहीं करता — दोनों variables memory में वही object refer करते हैं. एक बदलिए और "दूसरा" भी बदलता है, जो सबको एक बार चौंकाता है. यह वही reference व्यवहार है जो आप [1,2] === [1,2] false होने पर मिले. असली copy बनाने को spread operator {...obj} use कीजिए. Spreading merge भी करता है: बाद वाले objects पहले वालों को overwrite करते हैं, ठीक ऐसे ही आप defaults के ऊपर user preferences लगाते हैं. (ध्यान: यह "shallow" copy है — अंदर के nested objects अब भी साझा हैं.)

Objects की Arrays — सबसे common असली structure

let students = [
    { name: "Aman",  marks: 85 },
    { name: "Priya", marks: 92 },
    { name: "Ravi",  marks: 28 }
];

// Ab array methods ko objects ke saath milao:
let names = students.map(s => s.name);              // ["Aman","Priya","Ravi"]
let passed = students.filter(s => s.marks >= 33);   // do jo pass hue
let total = students.reduce((sum, s) => sum + s.marks, 0);   // 205
let topper = students.find(s => s.marks > 90);      // Priya object
Objects की array VAHI standard तरीका है जिसमें असली data आता है — databases से, APIs से, forms से. Students, products, orders, या posts की list हमेशा ऐसी ही आकार की होती है. और यहां पिछले कुछ chapters का फायदा है: सारे array methods इस पर खूबसूरती से चलते हैं. एक field निकालने को map, मिलते records चुनने को filter, column का total करने को reduce, एक record ढूंढने को find. यह combination — objects की arrays plus map/filter/reduce — JavaScript का सबसे ज़रूरी data-handling skill है.

व्यावहारिक Patterns

// Shorthand property names — jab key aur variable ka naam same ho:
let name = "Aman", marks = 85;
let student = { name, marks };        // { name: name, marks: marks } jaisa

// Objects ki array sort karna:
students.sort((a, b) => b.marks - a.marks);   // sabse zyada marks pehle

// Key maujood hai jaanchna use karne se pehle:
if ("marks" in student) { console.log(student.marks); }
तीन patterns जो आप लगातार use करेंगे. Shorthand properties: अगर variable का नाम पहले से key जैसा है, बस एक बार लिखिए — { name, marks }. Objects sort करना: array methods की compare-function trick reuse कीजिए, पर property compare कीजिए (b.marks - a.marks descending sort करता है). और keys जांचना in से पढ़ने से पहले undefined आश्चर्य रोकता है. Objects, arrays, और functions सब आपके पास होने से, अब आप DOM के लिए तैयार हैं — जहां JavaScript असली web pages बदलना शुरू करता है.

Exam Corner

Q: Object.keys(obj) क्या लौटाता है? Object के property names की array.

Q: Object की properties कैसे गिनते हैं? Object.keys(obj).length — objects में .length नहीं होती.

Q: क्या let b = a object copy करता है? नहीं — दोनों variables वही object point करते हैं. Copy को {...a} use कीजिए.

Q: दो objects कैसे merge करते हैं? { ...obj1, ...obj2 } — बाद की keys पहले वालों को overwrite करती हैं.

Q: Student objects की array से सारे नाम कैसे पाते हैं? students.map(s => s.name)
← Back to JavaScript Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
👁 Live Preview
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का console.log देखने के लिए F12 दबाइए.