JS डिस्ट्रक्चरिंग
In this page:
const [item1, item2] = array;
const { property1, property2 } = object;
const { property: alias = defaultValue } = object;
Array Destructuring
Array destructuring elements को सिर्फ उनकी position के आधार पर variables में assign करता है, पहला variable पहला element लेता है, दूसरा दूसरा element, वगैरह।
उदाहरण: Array Destructuring
const [first, second] = [10, 20];
console.log(first, second);
Object Destructuring
Object destructuring values को position के बजाय matching property names के आधार पर variables में assign करता है, जिससे आप object में उनके क्रम की परवाह किए बिना ठीक वही properties निकाल सकते हैं जो आपको चाहिए।
उदाहरण: Object Destructuring
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
const user = { name: "Sam", age: 30 };
// Destructure {name, age} out of `user`
// Destructure {name, age} out of `user`
const { name, age } = user;
// Print `name, age` to the console
// Print `name, age` to the console
console.log(name, age);
Default Values
array और object destructuring दोनों default values को support करते हैं, जो अपने आप तब लागू होती हैं जब संबंधित source value undefined हो, बिना अतिरिक्त conditional code के एक fallback देते हुए।
उदाहरण: Default Values
// Destructure {name = "Guest"} out of `{}`
// Destructure {name = "Guest"} out of `{}`
const { name = "Guest" } = {};
// Print `name` to the console
// Print `name` to the console
console.log(name);
// Destructure `[5]` into array elements a = 1, b = 2
// Destructure `[5]` into array elements a = 1, b = 2
const [a = 1, b = 2] = [5];
// Print `a, b` to the console
// Print `a, b` to the console
console.log(a, b);
Renaming और Nested Destructuring
Object destructuring आपको किसी variable को निकालते हुए rename करने देता है, property: newName इस्तेमाल करके, और सीधे nested objects के अंदर तक पहुंच सकता है, एक single statement में गहराई में छुपी values निकालते हुए।
उदाहरण: Renaming and Nested Destructuring
// Declare the constant `user`, set to `{ name: "Sam", address: { city: "Delhi" } }`
// Declare the constant `user`, set to `{ name: "Sam", address: { city: "Delhi" } }`
const user = { name: "Sam", address: { city: "Delhi" } };
const { name: userName, address: { city } } = user;
// Print `userName, city` to the console
// Print `userName, city` to the console
console.log(userName, city);
Destructuring में Rest Operator
destructuring pattern के अंदर rest operator ... बाकी बची, unmatched values या properties को एक नई array या object में इकट्ठा कर देता है, जिससे आप जो हिस्से explicitly चाहते हैं उन्हें बाकी सब से अलग कर सकते हैं।
उदाहरण: The Rest Operator in Destructuring
// Destructure `[1, 2, 3, 4]` into array elements first, ...rest
// Destructure `[1, 2, 3, 4]` into array elements first, ...rest
const [first, ...rest] = [1, 2, 3, 4];
// Print `first, rest` to the console
// Print `first, rest` to the console
console.log(first, rest);
// Destructure {id, ...others} out of `{ id: 1, name: "Sam", age: 30 }`
// Destructure {id, ...others} out of `{ id: 1, name: "Sam", age: 30 }`
const { id, ...others } = { id: 1, name: "Sam", age: 30 };
// Print `id, others` to the console
// Print `id, others` to the console
console.log(id, others);
- array destructuring के positional order को object destructuring के name-based matching से मिला देना, वे बहुत अलग rules follow करते हैं।
- यह भूल जाना कि object destructuring के दौरान कोई missing property error throw करने के बजाय बस undefined बन जाती है।
- deeply nested destructuring patterns को overcomplicate करना, जब value को सीधे access करना असल में ज़्यादा साफ होता।
- Array destructuring position के आधार पर values निकालता है, जबकि object destructuring matching property names के आधार पर values निकालता है।
- source value के missing होने की स्थिति में किसी भी destructured variable के लिए default values दी जा सकती हैं।
- Destructuring variables का नाम बदल सकता है, nested structures पर काम कर सकता है, और बाकी बची values capture करने के लिए rest operator इस्तेमाल कर सकता है।
Array और object destructuring को 2015 से हर major browser में support मिला है और यह modern JavaScript का पूरी तरह standard हिस्सा है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic