JS Object Management
In this page:
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 बदला भी जाता है और लौटाया भी जाता है।
उदाहरण: Merging Objects with Object.assign()
// 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 को कभी नहीं बदलता।
उदाहरण: Merging Objects with Spread Syntax
// 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 साझा करते हैं और उसे बदल सकते हैं।
उदाहरण: The Shallow Copy Limitation
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 के रूप में चाहने पर उपयोगी है।
उदाहरण: Building an Object from Entries with Object.fromEntries()
const pairs = [["a", 1], ["b", 2]];
console.log(Object.fromEntries(pairs));
Property की मौजूदगी जाँचना और Properties हटाना
in और hasOwnProperty() दोनों जाँचते हैं कि object पर कोई property मौजूद है या नहीं, और delete किसी property को object से पूरी तरह हटा देता है -- values को केवल पढ़ने और सेट करने से आगे object की संरचना को प्रबंधित करने के तीन पूरक औज़ार।
उदाहरण: Checking Property Existence and Removing Properties
// 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);
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: