JavaScript Interview Question For Beginners In Hindi
Logical Operators In JavaScript In Hindi | JS Logical Operators In Hindi
Promise In JavaScript In Hindi | JS Promise constructor
JavaScript Object keys, values, entries : Object methods in JS
JavaScript Event Loop In Hindi | Understand Event Loop In JS In Hindi
What is Modem ? and How Modem works?
प्रसिद्ध मुहावरे और कहावतें हिंदी में | Famous Muhaware In Hindi | Hindi Muhaware Part 1
Linux cat command Example | Linux print file content on terminal
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
JavaScript में अभी तक आपने data structures जैसे Object और Array के बारे में ही पढ़ा और समझा , लेकिन real life (Development) में आपको सिर्फ इन्ही दो से काम नहीं चलेगा।
इसलिए JavaScript ने Map और Set को provide किया है , और इस article में हम Set
के बारे में समझेंगे।
JavaScript में Set unique values का एक collection है , मतलब set में duplicate values नहीं हो सकती हैं। हालाँकि आप किसी भी Data Type की value को इसमें रख सकते हैं।
जिस order में आप किसी Set में values को insert करोगे उसी order में आपको values मिलेगी।
new Set()
– इसका use Set create करने के लिए किया जाता है।
set.size
– size property से set का size पता चलता है।
set.add(value)
– add method का use , value add करने के लिए किया जाता है।
set.has(value)
– has method से आप check कर सकते हैं कि कोई value exist करती है या नहीं।
set.delete(value)
– इसका use set से किसी particular value को delete करने के लिए किया जाता है।
set.clear()
– इस method से set को clear करने के लिए use करते हैं।
set.forEach(value)
- यह हर element के लिए एक Callback Function को Invoke / call करता है।
set.values()
- values() method एक Iterator object return करता है , जिसे आप for of Loop का use करके iterate कर सकते हैं।
Set को create करने के 2 तरीके हैं -
पहला कि Array को directly new Set()
में pass करके।
और दूसरा new Set() create करें और फिर value या variable को add()
method के through add करें।
const fruits = new Set(["Apple","Banana","Papaya"]);
console.log(fruits);
Set(3) [ "Apple", "Banana", "Papaya" ]
दूसरा आप directly values को add कर सकते हैं।
const fruits = new Set();
fruits.add("Apple");
fruits.add("Guava");
console.log(fruits);
Set [ "Apple", "Guava" ]
जैसा कि अभी आपने पढ़ा कि यह forEach()
method हर element के लिए एक Callback Function को Invoke / call करता है।
const fruits = new Set(["Apple","Banana","Papaya"]);
fruits.forEach(value => {
console.log(value);
});
Apple Banana Papaya
Example में Arrow Function use किया गया है , आप इसकी जगह कोई normal callback function भी use कर सकते हैं।
values()
method एक Iterator object return करता है , जिसे आप for of Loop का use करके iterate कर सकते हैं।
const fruits = new Set(["Apple","Banana","Papaya"]);
let iterator = fruits.values();
for(let fruit of iterator) console.log(fruit);
Apple Banana Papaya
has()
method से आप check कर सकते हैं कि कोई value exist करती है या नहीं।
if(fruits.has('Mango')) console.log("Mango exists");
else console.log("Mango does not exist.");
Mango does not exist.
size property से set का size पता चलता है।
console.log(fruits.size); // 3
clear()
method से set को करते हैं।
fruits.clear();
console.log(fruits); // Set []
delete()
method का use set से किसी particular value को delete करने के लिए किया जाता है। अगर set से value delete हो गयी तो true
return होगा otherwise false
.
const fruits = new Set(["Apple","Banana","Papaya"]);
console.log(fruits.delete("Apple"));
console.log(fruits.delete("Mango"));
true false
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook