JS सेट्स
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 बचती हैं।
उदाहरण: Creating a Set
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 में पहले से मौजूद है या नहीं।
उदाहरण: add, delete, and has
// 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 को दिखाती है।
उदाहरण: The size Property
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 के इर्द-गिर्द बने हों।
उदाहरण: Iterating a Set
// 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 के पास नहीं हैं।
उदाहरण: Set vs Array
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);
- किसी Set के items को index से access करने की कोशिश करना, जैसे mySet[0], Sets में कोई index-based access नहीं है, सिर्फ iteration और has()।
- यह भूल जाना कि किसी Set का size size property से check होता है, array की तरह length से नहीं।
- यह मान लेना कि 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 हिस्सा है।
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