Set metodlari
Set metodlari va xususiyatlari
Yangi Set() usuli
new Set() konstruktoriga massivni uzating:
add() usuli
Agar siz teng elementlarni qo‘shsangiz, faqat birinchisi saqlanadi:
Misol
letters.add("a");
letters.add("b");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
O‘zingiz sinab ko‘ring »
Set obyektlarining asosiy xususiyati shundaki, ular faqat noyob qiymatlarni saqlaydi.
Agar Setda allaqachon mavjud bo‘lgan elementni qo‘shishga urinilsa, add() metodi hech qanday ta’sir ko‘rsatmaydi va Set o‘zgarishsiz qoladi.
Xususiyatning o‘lchami
Misol
// Create a new Set
const mySet = new Set(["a","b","c"]);
// The number of elements are
mySet.size;
O‘zingiz sinab ko‘ring »
Set elementlarini sanab chiqish
Siz barcha Set elementlarini (qiymatlarini) for..of sikli bilan ro‘yxatga olishingiz mumkin:
Misol
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Elements
let text = "";
for (const x of letters) {
text += x;
}
O‘zingiz sinab ko‘ring »
has() metodi
has() usuli, agar to‘plamda belgilangan qiymat mavjud bo‘lsa, true qaytaradi.
Misol
// Create a Set
const letters = new Set(["a","b","c"]);
// Does the Set contain "d"?
answer = letters.has("d");
O‘zingiz sinab ko‘ring »
forEach() metodi
forEach() usuli har bir Set elementi uchun funksiyani chaqiradi:
Misol
// Create a Set
const letters = new Set(["a","b","c"]);
// List all entries
let text = "";
letters.forEach (function(value) {
text += value;
})
O‘zingiz sinab ko‘ring »
values() metodi
values() usuli to‘plamdagi qiymatlarga ega Iterator obyektini qaytaradi:
1-misol
// Create a Set
const letters = new Set(["a","b","c"]);
// Get all Values
const myIterator = letters.values();
// List all Values
let text = "";
for (const entry of myIterator) {
text += entry;
}
O‘zingiz sinab ko‘ring »
2-misol
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Values
let text = "";
for (const entry of letters.values()) {
text += entry;
}
O‘zingiz sinab ko‘ring »
Keys() usuli
keys() usuli to‘plamdagi qiymatlarga ega Iterator obyektini qaytaradi:
To‘plamda kalitlar yo‘q, shuning uchun keys() values() kabi qaytaradi.
Bu to‘plamlarni Map’lar bilan mos qiladi.
1-misol
// Create a Set
const letters = new Set(["a","b","c"]);
// Create an Iterator
const myIterator = letters.keys();
// List all Elements
let text = "";
for (const x of myIterator) {
text += x;
}
O‘zingiz sinab ko‘ring »
2-misol
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Elements
let text = "";
for (const x of letters.keys()) {
text += x;
}
O‘zingiz sinab ko‘ring »
entries() metodi
entries() metodi setdan [value, value] juftliklari bilan Iteratorni qaytaradi.
entries() usuli obyektdan [kalit, qiymat] juftligini qaytarishi kerak.
To‘plamda kalitlar yo‘q, shuning uchun entries() usuli [qiymat, qiymat] ni qaytaradi.
Bu to‘plamlarni Map’lar bilan mos qiladi.
1-misol
// Create a Set
const letters = new Set(["a","b","c"]);
// Get all Entries
const myIterator = letters.entries();
// List all Entries
let text = "";
for (const entry of myIterator) {
text += entry;
}
O‘zingiz sinab ko‘ring »
2-misol
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Entries
let text = "";
for (const entry of letters.entries()) {
text += entry;
}
O‘zingiz sinab ko‘ring »
Batafsil bilib oling:
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
