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

JS Object Management

Beyond creating and reading individual properties, JavaScript offers tools for working with objects as a whole -- Object.assign() to merge several objects together, the spread operator for a similar but more concise merge, and Object.fromEntries() to build an object back up from an array of pairs.
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. #}
🔒

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.