किसी ऑब्जेक्ट की प्रॉपर्टीज का सबसेट लेकर नया ऑब्जेक्ट बनाने के लिए ऑब्जेक्ट डिस्ट्रक्चरिंग और प्रॉपर्टी शॉर्टहैंड का उपयोग करें।
उदाहरण
const person = {
name: 'John',
age: 40,
city: 'LA',
school: 'High School'
};
यदि आपको केवल name और age चाहिए:
const { name, age } = person;
const selectedObj = { name, age };
console.log(selectedObj);
आउटपुट
{ name: 'John', age: 40 }
उन्नत पैटर्न्स
रेस्ट ऑपरेटर के साथ बाकी प्रॉपर्टीज निकालना
const { name, age, ...rest } = person;
console.log(rest); // { city: 'LA', school: 'High School' }
रीनेमिंग के साथ
const { name: fullName, age: years } = person;
const selectedObj = { fullName, years };
नेस्टेड ऑब्जेक्ट्स के लिए
const user = {
id: 1,
profile: { firstName: 'John', lastName: 'Doe' },
settings: { theme: 'dark' }
};
const { profile: { firstName, lastName } } = user;
// firstName, lastName अब उपलब्ध हैं