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

JS सेट्स

किसी event के दरवाज़े पर मौजूद guest list के बारे में सोचें, जोड़े जाने से पहले हर नाम जांचा जाता है, और कोई नाम कभी दो बार नहीं आता, चाहे कोई कितनी भी बार खुद को दोबारा जोड़ने की कोशिश करे। JavaScript में एक Set बिल्कुल वही guest list है, एक ऐसा collection जो अपने आप uniqueness लागू करता है, पहले से मौजूद value को जोड़ने की कोशिश करने पर कुछ नहीं होता, और collection चुपचाप duplicates से मुक्त रहता है। जहाँ भी आपको सच में distinct values की एक list चाहिए, वहां Sets काम आते हैं, जैसे यह track करना कि cookiescursor.com के किसी visitor ने कौन से unique tutorial topics देखे हैं।
Syntax
javascript
const setName = new Set([item1, item2]);
setName.add(item);
setName.has(item);
setName.delete(item);

Set बनाना

एक Set new Set() से बनाया जाता है, चाहें तो किसी array जैसा iterable भी पास किया जा सकता है, और उस initial array में मौजूद कोई भी duplicate values अपने आप हटा दी जाती हैं, सिर्फ unique entries बचती हैं।

Note: किसी मौजूदा array को new Set() में पास करना उस array से duplicate values हटाने का एक जल्दी, आम तरीका है।
Warning: एक Set अपनी values के type को सख्ती से बनाए रखता है, number 1 और string 1 को दो अलग-अलग, distinct entries माना जाता है।

उदाहरण: Creating a Set

javascript
const nums = new Set([1, 2, 2, 3]);
console.log(nums); // duplicates dropped: {1, 2, 3}

add, delete, और has

add() Set में एक नई value जोड़ता है, delete() किसी खास value को हटाता है और यह return करता है कि वह असल में मौजूद थी या नहीं, और has() जांचता है कि दी गई value Set में पहले से मौजूद है या नहीं।

Note: has() किसी Set पर बहुत कुशलता से चलता है, जिससे array.includes() के मुकाबले किसी बड़े collection में membership जांचने के लिए यह एक बढ़िया विकल्प बन जाता है।
Warning: पहले से Set में मौजूद value के साथ add() कॉल करने से कुछ नहीं होता और कोई error भी नहीं आता, Set चुपचाप वैसा ही रहता है।

उदाहरण: add, delete, and has

javascript
// Declare the constant `nums` as a new `Set` instance
// Declare the constant `nums` as a new `Set` instance
const nums = new Set();
// Call `nums.add(1)`
// Call `nums.add(1)`
nums.add(1);
// Call `nums.add(2)`
// Call `nums.add(2)`
nums.add(2);
// Print `nums.has(1)` to the console
// Print `nums.has(1)` to the console
console.log(nums.has(1));
// Call `nums.delete(1)`
// Call `nums.delete(1)`
nums.delete(1);
// Print `nums.has(1)` to the console
// Print `nums.has(1)` to the console
console.log(nums.has(1));

size Property

किसी Set की size property बताती है कि उसमें फिलहाल कितनी unique values हैं, जो arrays और strings के लिए length जैसा ही role निभाती है। array की length के उलट, size read-only है और हमेशा Set के मौजूदा असली content को दिखाती है।

Note: किसी array की length को उससे बनाए गए Set से compare करना, जैसे new Set(array).size, यह जांचने का एक जल्दी तरीका है कि array में कोई duplicates थे या नहीं।
Warning: size एक property है, method नहीं, इसे बिना parentheses के access किया जाता है, mySet.size, न कि mySet.size()।

उदाहरण: The size Property

javascript
const nums = new Set([1, 2, 3]);
console.log(nums.size);

Set को Iterate करना

Sets iterable होते हैं, जो आपको for...of से हर unique value पर उसी क्रम में loop करने देते हैं जिस क्रम में values शुरू में जोड़ी गई थीं।

यह insertion-order की गारंटी Sets को iterate करने में predictable बनाती है, भले ही वे position के बजाय uniqueness के इर्द-गिर्द बने हों।

Note: Sets insertion order बनाए रखते हैं, इसलिए किसी को iterate करने पर values हमेशा उसी क्रम में मिलती हैं जिस क्रम में वे पहली बार जोड़ी गई थीं।
Warning: आप किसी Set पर mySet[i] जैसा regular indexed for loop इस्तेमाल नहीं कर सकते, इसमें कोई index-based access है ही नहीं।

उदाहरण: Iterating a Set

javascript
// Declare the constant `nums` as a new `Set` instance
// Declare the constant `nums` as a new `Set` instance
const nums = new Set([1, 2, 3]);
// Loop over `nums`, binding each item to `n`
// Loop over `nums`, binding each item to `n`
for (let n of nums) {
  // Print `n` to the console
  // Print `n` to the console
  console.log(n);
}

Set बनाम Array

एक Set अपने आप uniqueness लागू करता है और has() से बहुत तेज़ membership checks देता है, जबकि एक array duplicates की इजाज़त देता है और map, filter, और indexed access जैसे rich methods support करता है जो Sets के पास नहीं हैं।

Note: जब भी आपको map या filter जैसे array-specific methods चाहिए हों, spread operator से किसी Set को वापस array में बदल दें, [...mySet]।
Warning: सिर्फ uniqueness के लिए Set चुनना और फिर बार-बार array-style operations की ज़रूरत पड़ना अक्सर बार-बार आगे-पीछे convert करने का मतलब बन जाता है, विचार करें कि क्या manual duplicate checks वाला array ज़्यादा सरल हो सकता है।

उदाहरण: Set vs Array

javascript
const set = new Set([1, 2, 2, 3]);
const arr = [1, 2, 2, 3];
console.log(set.size, arr.length); // 3 vs 4
console.log(set.has(2), arr.indexOf(2) !== -1);
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 के items को index से access करने की कोशिश करना, जैसे mySet[0], Sets में कोई index-based access नहीं है, सिर्फ iteration और has()।
  2. यह भूल जाना कि किसी Set का size size property से check होता है, array की तरह length से नहीं।
  3. यह मान लेना कि duplicate value जोड़ने पर error throw होगा, ऐसा नहीं होता, existing value पर add() का बस कोई असर नहीं होता।
चैप्टर सारांश
  • एक Set values का एक collection store करता है जहाँ हर value unique होने की guarantee है।
  • add(), delete(), और has() किसी Set के contents manage करने के मुख्य methods हैं।
  • Sets को for...of से iterate किया जा सकता है और array methods चाहिए होने पर spread operator से arrays में convert किया जा सकता है।
ब्राउज़र सपोर्ट

Set को 2015 से सभी major browsers में support मिला है और यह modern JavaScript का पूरी तरह standard हिस्सा है।

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.