JS स्प्रेड ऑपरेटर
In this page:
const newArray = [...array1, ...array2];
const newObject = { ...object1, ...object2 };
functionName(...array);
Spread Syntax क्या है
Spread operator ... किसी iterable जैसे array या string को उसके individual elements में फैला देता है, जहाँ भी कई values की उम्मीद हो, जैसे किसी array literal या function call के अंदर।
उदाहरण: What Is Spread Syntax
const nums = [1, 2, 3];
console.log(...nums); // expands array into individual values
console.log([...nums]);
Arrays को Combine करना
दो या ज़्यादा arrays को एक नए array literal में spread करना, जैसे [...a, ...b], उनके elements को बिना किसी original array को mutate किए एक नई single array में जोड़ देता है, और आपको mix में कहीं भी अतिरिक्त values डालने देता है।
उदाहरण: Combining Arrays
// Declare the constant `a`, set to `[1, 2]`
// Declare the constant `a`, set to `[1, 2]`
const a = [1, 2];
// Declare the constant `b`, set to `[3, 4]`
// Declare the constant `b`, set to `[3, 4]`
const b = [3, 4];
// Declare the constant `combined`, set to `[...a, ...b, 5]`
// Declare the constant `combined`, set to `[...a, ...b, 5]`
const combined = [...a, ...b, 5];
// Print `combined` to the console
// Print `combined` to the console
console.log(combined);
Objects को Copy करना
{...obj} से किसी object को spread करना उसकी अपनी enumerable properties को एक नए object में copy कर देता है, यह original को छुए बिना एक updated copy बनाने का एक आम pattern है, जिसे अक्सर खास fields को override करने के लिए नई properties के साथ मिलाया जाता है।
उदाहरण: Copying Objects
// Declare the constant `obj`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `obj`, set to `{ name: "Sam", age: 30 }`
const obj = { name: "Sam", age: 30 };
// Declare the constant `copy`, set to `{ ...obj, age: 31 }`
// Declare the constant `copy`, set to `{ ...obj, age: 31 }`
const copy = { ...obj, age: 31 };
// Print `copy` to the console
// Print `copy` to the console
console.log(copy);
Function Calls के साथ Spread
Spread function calls के अंदर भी काम करता है, [1, 2, 3] जैसी किसी array को तीन अलग-अलग arguments में बदल देता है, इसलिए Math.max(...numbers) array को खुद पास करने के बजाय हर number को अलग-अलग पास करता है।
उदाहरण: Spread with Function Calls
const numbers = [1, 5, 3];
console.log(Math.max(...numbers));
Shallow Copy की सीमा
Spread सिर्फ एक level गहराई तक copy करता है, इसलिए spread की गई value के अंदर nested objects या arrays reference से copy होते हैं, यानी किसी nested object में बदलाव original और copy दोनों को प्रभावित करते हैं।
उदाहरण: Shallow Copy Limitation
const original = { info: { city: "Delhi" } };
const copy = { ...original };
copy.info.city = "Mumbai";
console.log(original.info.city); // "Mumbai" - nested object is shared
- यह उम्मीद करना कि spread एक deep copy बनाता है, जबकि
{...obj}सिर्फ top level copy करता है और nested objects अभी भी shared रहते हैं। - किसी non-iterable को array में spread करना, जैसे
[...5], जोTypeErrorthrow करता है। - किसी object में spread से पहले properties रखना, जैसे
{a: 1, ...obj}, इसलिएobjमें मौजूद value आपकी पहले वाली को override कर देती है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: