← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 26 of 26

JS Set लॉजिक

individual items जोड़ने और हटाने से आगे, JavaScript Sets सीधे mathematical set operations support करते हैं -- union (दोनों में से किसी में भी जो कुछ है), intersection (सिर्फ जो दोनों में है), और difference (जो एक में है पर दूसरे में नहीं) -- जो आपको बिना manual loop-based logic लिखे दो Sets को मिलाने या compare करने देते हैं।
Syntax
javascript
const union = new Set([...setA, ...setB]);
const intersection = new Set([...setA].filter(x => setB.has(x)));
const difference = new Set([...setA].filter(x => !setB.has(x)));

Union: दो Sets को Combine करना

setA.union(setB) एक बिल्कुल नया Set return करता है जिसमें दोनों Sets के मिलाए गए हर unique item होता है -- बिल्कुल वैसे जैसे दो lists को मिलाते हुए किसी भी duplicates को अपने आप हटाना, क्योंकि एक Set कभी एक जैसी value दो बार नहीं रख सकता।

Note: union() का इस्तेमाल तब करें जब आपको दो collections के हर distinct item को मिलाकर चाहिए, Set की अंतर्निहित uniqueness को deduplication अपने आप संभालने देते हुए।
Warning: union() (बाकी set operations की तरह) एक नया Set return करता है, दोनों original Sets को पूरी तरह अनबदला छोड़ते हुए।

उदाहरण: Union: Combining Two Sets

javascript
const a = new Set([1, 2, 3]);
const b = new Set([3, 4, 5]);
console.log(a.union(b)); // {1, 2, 3, 4, 5}

Intersection: Shared Items ढूंढना

setA.intersection(setB) एक नया Set return करता है जिसमें सिर्फ वही items होते हैं जो दोनों Sets में मौजूद हैं -- यह common elements ढूंढने के लिए उपयोगी है, जैसे दो articles के बीच साझा tags, या दो अलग-अलग groups में मौजूद users।

Note: intersection() का इस्तेमाल खासतौर पर तब करें जब आपको यह जानना हो कि दो collections में क्या common है, न कि किसी एक से सब कुछ।
Warning: कुछ भी common न रखने वाले दो Sets के बीच intersection सही तरीके से एक खाली Set return करता है, कोई error या null नहीं।

उदाहरण: Intersection: Finding Shared Items

javascript
const a = new Set([1, 2, 3]);
const b = new Set([2, 3, 4]);
console.log(a.intersection(b)); // {2, 3}

Difference: किसी एक Set की अपनी चीज़ें ढूंढना

setA.difference(setB) एक नया Set return करता है जिसमें सिर्फ वही items होते हैं जो setA में हैं पर setB में नहीं -- यह यह ढूंढने के लिए उपयोगी है कि क्या हटाया गया, क्या process होना बाकी है, या एक group के पास क्या है जो दूसरे के पास नहीं, सिर्फ एक दिशा में।

Note: याद रखें कि difference() directional है: setA.difference(setB), setB.difference(setA) जैसा result नहीं है -- union या intersection के उलट, यहाँ क्रम मायने रखता है।
Warning: जब असल में आपका मतलब setB.difference(setA) हो तो setA.difference(setB) का क्रम मिला देना एक आसान गलती है, क्योंकि सिर्फ method का नाम दिशा नहीं बताता।

उदाहरण: Difference: Finding What's Unique to One Set

javascript
const a = new Set([1, 2, 3]);
const b = new Set([2, 3]);
console.log(a.difference(b)); // {1}

अन्य Set Relationship Checks

isSubsetOf(), isSupersetOf(), और isDisjointFrom() इस बारे में खास yes/no सवालों के जवाब देते हैं कि दो Sets कैसे संबंधित हैं -- क्या एक पूरी तरह दूसरे के अंदर है, क्या एक पूरी तरह दूसरे को समाहित करता है, या क्या वे बिल्कुल कोई elements साझा नहीं करते।

Note: इन relationship-check methods का इस्तेमाल तब करें जब आपको खासतौर पर यह true/false जवाब चाहिए कि दो Sets कैसे संबंधित हैं, खुद एक combined Set calculate और inspect करने के बजाय।
Warning: isSubsetOf() और isSupersetOf() एक ही relationship के उलटे नज़रिए हैं -- setA.isSubsetOf(setB), setB.isSupersetOf(setA) के बराबर है।

उदाहरण: Other Set Relationship Checks

javascript
const a = new Set([1, 2]);
const b = new Set([1, 2, 3]);
console.log(a.isSubsetOf(b));   // true
console.log(b.isSupersetOf(a)); // true
console.log(a.isDisjointFrom(new Set([9]))); // true

पुराने Browsers के लिए Fallback

चूंकि ये set-operation methods अपेक्षाकृत नया addition हैं, पुराने browsers को support करने की ज़रूरत वाला code array methods और manual filtering से वही results पा सकता है -- union, intersection, और difference व्यवहार को manually दोहराने के लिए spread syntax को .filter() के साथ मिलाकर।

Note: अगर व्यापक पुराने-browser support एक असली ज़रूरत है, तो नए built-in methods के बजाय manual array-based fallback इस्तेमाल करें, या एक छोटा polyfill जोड़ें।
Warning: browser support requirements जांचे बिना चुपचाप union()/intersection()/difference() पर निर्भर रहना उन पुराने browsers पर एक runtime error ("not a function") दे सकता है जिनमें ये नए methods नहीं हैं।

उदाहरण: Fallback for Older Browsers

javascript
const a = [1, 2, 3];
const b = [2, 3, 4];
const union = [...new Set([...a, ...b])];
const intersection = a.filter(x => b.includes(x));
console.log(union, intersection); // manual fallback for older browsers
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. #}
आम गलतियां
  1. यह मान लेना कि set-operation methods (union, intersection, difference) हर जगह supported हैं -- ये एक relatively recent addition हैं, और पुराने browsers को इसके बजाय एक manual loop-based implementation चाहिए हो सकता है।
  2. intersection (सिर्फ shared items) को union (दोनों की सारी चीज़ें) से confuse करना, जिससे गलत combined result मिलता है।
  3. यह भूल जाना कि ये methods एक नया Set return करते हैं, original Sets में से किसी को भी in place modify करने के बजाय।
चैप्टर सारांश
  • setA.union(setB) एक नया Set return करता है जिसमें दोनों Sets में मौजूद हर item होता है।
  • setA.intersection(setB) एक नया Set return करता है जिसमें सिर्फ वे items होते हैं जो दोनों Sets में मौजूद हैं।
  • setA.difference(setB) एक नया Set return करता है जिसमें वे items होते हैं जो setA में हैं लेकिन setB में नहीं।
ब्राउज़र सपोर्ट

union(), intersection(), और difference() जैसे Set methods ES2024/ES2025 में जोड़े गए थे और सभी major browsers के current versions में supported हैं; पुराने browsers को एक manual loop-based fallback चाहिए।

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.