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

JS Object मैनेजमेंट

individual properties बनाने और पढ़ने से आगे, JavaScript objects को पूरे के रूप में काम करने के लिए tools देता है -- कई objects को मिलाने के लिए Object.assign(), एक जैसा पर ज़्यादा concise merge के लिए spread operator, और pairs की एक array से वापस एक object बनाने के लिए Object.fromEntries()।
Syntax
javascript
Object.assign(target, source);
Object.keys(object);
Object.entries(object);
delete object.property;

Object.assign() से Objects को मिलाना

Object.assign(target, source1, source2, ...) हर source object की हर enumerable अपनी property को target object में क्रम से कॉपी करता है -- बाद के sources की properties पहले के sources की उसी नाम वाली properties को अधिलेखित करती हैं, और संशोधित target बदला भी जाता है और लौटाया भी जाता है।

Note: जब आप किसी भी मूल को बदले बिना नया मिलाया हुआ object चाहते हों तो target के रूप में खाली object literal ({}) दीजिए: Object.assign({}, obj1, obj2)।
Warning: Object.assign(obj1, obj2) mutates obj1 directly, which can be surprising if you expected it to leave both original objects untouched.

उदाहरण: Merging Objects with Object.assign()

javascript
// Declare the constant `defaults`, set to `{ theme: "light" }`
// Declare the constant `defaults`, set to `{ theme: "light" }`
const defaults = { theme: "light" };
// Declare the constant `overrides`, set to `{ theme: "dark" }`
// Declare the constant `overrides`, set to `{ theme: "dark" }`
const overrides = { theme: "dark" };
// Declare the constant `merged`, set to `Object.assign({}, defaults, overrides)`
// Declare the constant `merged`, set to `Object.assign({}, defaults, overrides)`
const merged = Object.assign({}, defaults, overrides);
// Print `merged` to the console
// Print `merged` to the console
console.log(merged);

Spread Syntax से Objects को मिलाना

Object literal के भीतर spread operator, {...obj1, ...obj2}, Object.assign() जैसा ही मिलाने का व्यवहार ज़्यादा संक्षिप्त, पठनीय syntax से हासिल करता है -- और हमेशा एकदम नया object बनाता है, किसी भी source object को कभी नहीं बदलता।

Note: नए code में मिलाने के लिए Object.assign() की जगह spread syntax को तरजीह दीजिए, क्योंकि यह ज़्यादा साफ़ पढ़ा जाता है और मौजूदा object को अनजाने में बदलने का जोखिम नहीं उठाता।
Warning: Like Object.assign(), spread-based merging only copies properties one level deep -- nested objects remain shared references, not independent copies.

उदाहरण: Merging Objects with Spread Syntax

javascript
// Declare the constant `defaults`, set to `{ theme: "light" }`
// Declare the constant `defaults`, set to `{ theme: "light" }`
const defaults = { theme: "light" };
// Declare the constant `overrides`, set to `{ theme: "dark" }`
// Declare the constant `overrides`, set to `{ theme: "dark" }`
const overrides = { theme: "dark" };
// Declare the constant `merged`, set to `{ ...defaults, ...overrides }`
// Declare the constant `merged`, set to `{ ...defaults, ...overrides }`
const merged = { ...defaults, ...overrides };
// Print `merged` to the console
// Print `merged` to the console
console.log(merged);

Shallow Copy की सीमा

Object.assign() और spread syntax दोनों shallow copy करते हैं -- शीर्ष-स्तर की properties सचमुच कॉपी होती हैं, लेकिन अगर किसी property का value खुद कोई object या array है, तो केवल उस nested संरचना का संदर्भ कॉपी होता है, यानी मूल और नक़ल अब भी वही nested data साझा करते हैं और उसे बदल सकते हैं।

Note: जब सचमुच गहरी, पूरी तरह स्वतंत्र नक़ल चाहिए (nested objects समेत), तो Object.assign() या spread की जगह structuredClone() (आधुनिक browsers में built-in) का उपयोग कीजिए।
Warning: Modifying a nested object through the "copy" can unexpectedly also change the original, since the shallow copy only duplicated the outer object, not the nested one inside it.

उदाहरण: The Shallow Copy Limitation

javascript
const original = { info: { city: "Delhi" } };
const copy = { ...original };
copy.info.city = "Mumbai";
console.log(original.info.city); // "Mumbai" - nested object shared

Object.fromEntries() से Entries से Object बनाना

Object.fromEntries(arrayOfPairs) Object.entries() की उलटी क्रिया है -- यह [key, value] जोड़ियों का array लेकर उनसे साधारण object बनाता है, जो array methods (जैसे filter या map) से data बदलने के बाद नतीजा वापस object के रूप में चाहने पर उपयोगी है।

Note: Object.entries(), array methods और Object.fromEntries() को एक आम पैटर्न के रूप में मिलाइए: object को जोड़ियों में बदलिए, जोड़ियों को array methods से बदलिए, फिर वापस object में बदल दीजिए।
Warning: Object.fromEntries() expects genuinely two-item [key, value] pairs -- passing malformed pairs (missing a value, for instance) produces properties set to undefined rather than an error.

उदाहरण: Building an Object from Entries with Object.fromEntries()

javascript
const pairs = [["a", 1], ["b", 2]];
console.log(Object.fromEntries(pairs));

Property की मौजूदगी जाँचना और Properties हटाना

in और hasOwnProperty() दोनों जाँचते हैं कि object पर कोई property मौजूद है या नहीं, और delete किसी property को object से पूरी तरह हटा देता है -- values को केवल पढ़ने और सेट करने से आगे object की संरचना को प्रबंधित करने के तीन पूरक औज़ार।

Note: जब आप विशेष रूप से जाँच से विरासत में मिली properties को बाहर रखना चाहते हों, तब in operator की बजाय obj.hasOwnProperty(key) को तरजीह दीजिए।
Warning: delete removes a property but does not affect other references to the same object elsewhere in your code -- all of them will see the property gone, since there is still only one underlying object.

उदाहरण: Checking Property Existence and Removing Properties

javascript
// Declare the constant `user`, set to `{ name: "Sam" }`
// Declare the constant `user`, set to `{ name: "Sam" }`
const user = { name: "Sam" };
// Print `"name" in user` to the console
// Print `"name" in user` to the console
console.log("name" in user);
// Print `user.hasOwnProperty("name")` to the console
// Print `user.hasOwnProperty("name")` to the console
console.log(user.hasOwnProperty("name"));
delete user.name;
// Print `user` to the console
// Print `user` to the console
console.log(user);
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. #}
आम गलतियां
  1. यह मान लेना कि Object.assign() या spread syntax एक deep copy करता है -- दोनों सिर्फ properties को एक level गहराई तक copy करते हैं, इसलिए कोई nested object या array original और copy के बीच अभी भी reference से shared रहता है।
  2. यह भूल जाना कि Object.assign() के बाद के arguments (या बाद की spread values) property names टकराने पर पहले वालों को overwrite कर देते हैं, और intended merge priority को उल्टा समझ लेना।
  3. Object.assign(target, source) इस्तेमाल करना और यह भूल जाना कि यह target object को सीधे mutate करता है -- पहले argument की तरह एक empty object literal pass करना किसी existing object को गलती से mutate होने से बचाता है।
चैप्टर सारांश
  • Object.assign(target, ...sources) एक या ज़्यादा source objects से properties को किसी target object में copy करता है, और वह target return करता है।
  • Spread operator ({...obj1, ...obj2}) ज़्यादा concise syntax के साथ similar merging behavior देता है।
  • Object.assign() और spread दोनों एक shallow copy करते हैं -- nested objects/arrays reference से shared रहते हैं, duplicate नहीं होते।
ब्राउज़र सपोर्ट

Object.assign() और object spread syntax दोनों हर modern browser में supported हैं (objects के लिए spread syntax खासतौर पर ES2018 से)।

🔒

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.