JavaScriptda massiv (array) bilan ishlash uchun ko’plab metodlar mavjud. Quyida asosiy array metodlari bilan tanishamiz:
push()
- Massivning oxiriga element qo’shadi.pop()
- Massivning oxiridan elementni olib tashlaydi.shift()
- Massivning boshidan elementni olib tashlaydi.unshift()
- Massivning boshiga element qo’shadi.concat()
- Ikki yoki undan ortiq massivlarni birlashtiradi.slice()
- Massivning bir qismini yangi massiv sifatida qaytaradi.splice()
- Massivga element qo’shadi yoki elementlarni olib tashlaydi.indexOf()
- Berilgan elementning massivdagi birinchi indeksini qaytaradi.lastIndexOf()
- Berilgan elementning massivdagi oxirgi indeksini qaytaradi.includes()
- Massivda berilgan element bor-yo’qligini tekshiradi.forEach()
- Har bir element uchun funksiya chaqiradi.map()
- Har bir element uchun funksiya chaqiradi va yangi massiv qaytaradi.filter()
- Berilgan shartga mos keluvchi elementlar bilan yangi massiv yaratadi.reduce()
- Massiv elementlarini bitta qiymatga kamaytiradi (agregatsiya).reduceRight()
-reduce
metodi bilan bir xil, lekin o’ngdan chapga agregatsiya qiladi.find()
- Shartga mos keladigan birinchi elementni qaytaradi.findIndex()
- Shartga mos keladigan birinchi elementning indeksini qaytaradi.every()
- Har bir element berilgan shartga mos kelishini tekshiradi.some()
- Hech bo’lmaganda bitta element berilgan shartga mos kelishini tekshiradi.sort()
- Massiv elementlarini tartiblaydi.reverse()
- Massiv elementlarini teskari tartiblaydi.join()
- Massiv elementlarini birlashtirib, bitta string hosil qiladi.flat()
- Ichma-ich joylashgan massivlarni tekislaydi.flatMap()
-map()
vaflat()
funksiyalarini birlashtiradi.fill()
- Massivni berilgan qiymat bilan to’ldiradi.from()
- Ob’ektdan yangi massiv yaratadi.isArray()
- Berilgan qiymat massiv ekanligini tekshiradi.of()
- Berilgan elementlardan yangi massiv yaratadi.copyWithin()
- Massiv ichida elementlarni ko’chiradi.entries()
- Massivning elementlari uchun[key, value]
juftligini qaytaradi.keys()
- Massivdagi indekslar bo’yicha iterator yaratadi.values()
- Massiv elementlari bo’yicha iterator yaratadi.
Keling har bir metod uchun misollarda ko’rib chiqamiz:
1. push()
let fruits = ['apple', 'banana'];
fruits.push('orange');
console.log(fruits); // ['apple', 'banana', 'orange']
2. pop()
let fruits = ['apple', 'banana', 'orange'];
fruits.pop();
console.log(fruits); // ['apple', 'banana']
3. shift()
let fruits = ['apple', 'banana', 'orange'];
fruits.shift();
console.log(fruits); // ['banana', 'orange']
4. unshift()
let fruits = ['banana', 'orange'];
fruits.unshift('apple');
console.log(fruits); // ['apple', 'banana', 'orange']
5. concat()
let fruits = ['apple', 'banana'];
let moreFruits = ['orange', 'mango'];
let allFruits = fruits.concat(moreFruits);
console.log(allFruits); // ['apple', 'banana', 'orange', 'mango']
6. slice()
let fruits = ['apple', 'banana', 'orange', 'mango'];
let slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits); // ['banana', 'orange']
7. splice()
let fruits = ['apple', 'banana', 'orange'];
fruits.splice(1, 1, 'mango');
console.log(fruits); // ['apple', 'mango', 'orange']
8. indexOf()
let fruits = ['apple', 'banana', 'orange'];
let index = fruits.indexOf('banana');
console.log(index); // 1
9. lastIndexOf()
let fruits = ['apple', 'banana', 'orange', 'banana'];
let index = fruits.lastIndexOf('banana');
console.log(index); // 3
10. includes()
let fruits = ['apple', 'banana', 'orange'];
let hasBanana = fruits.includes('banana');
console.log(hasBanana); // true
11. forEach()
let fruits = ['apple', 'banana', 'orange'];
fruits.forEach((fruit) => console.log(fruit));
// apple
// banana
// orange
12. map()
let numbers = [1, 2, 3];
let doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
13. filter()
let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]
14. reduce()
let numbers = [1, 2, 3, 4];
let sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum); // 10
15. reduceRight()
let numbers = [1, 2, 3, 4];
let difference = numbers.reduceRight((total, num) => total - num);
console.log(difference); // -2
16. find()
let numbers = [1, 2, 3, 4, 5];
let found = numbers.find(num => num > 3);
console.log(found); // 4
17. findIndex()
let numbers = [1, 2, 3, 4, 5];
let foundIndex = numbers.findIndex(num => num > 3);
console.log(foundIndex); // 3
18. every()
let numbers = [2, 4, 6, 8];
let allEven = numbers.every(num => num % 2 === 0);
console.log(allEven); // true
19. some()
let numbers = [1, 2, 3, 4, 5];
let hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven); // true
20. sort()
let fruits = ['banana', 'apple', 'orange'];
fruits.sort();
console.log(fruits); // ['apple', 'banana', 'orange']
21. reverse()
let fruits = ['banana', 'apple', 'orange'];
fruits.reverse();
console.log(fruits); // ['orange', 'apple', 'banana']
22. join()
let fruits = ['apple', 'banana', 'orange'];
let joinedFruits = fruits.join(', ');
console.log(joinedFruits); // 'apple, banana, orange'
23. flat()
let numbers = [1, [2, 3], [4, [5, 6]]];
let flatNumbers = numbers.flat(2);
console.log(flatNumbers); // [1, 2, 3, 4, 5, 6]
24. flatMap()
let numbers = [1, 2, 3];
let flatMapped = numbers.flatMap(num => [num, num * 2]);
console.log(flatMapped); // [1, 2, 2, 4, 3, 6]
25. fill()
let numbers = [1, 2, 3, 4];
numbers.fill(0, 1, 3);
console.log(numbers); // [1, 0, 0, 4]
26. from()
let str = 'hello';
let arrayFromString = Array.from(str);
console.log(arrayFromString); // ['h', 'e', 'l', 'l', 'o']
27. isArray()
let isArray = Array.isArray([1, 2, 3]);
console.log(isArray); // true
28. of()
let array = Array.of(1, 2, 3);
console.log(array); // [1, 2, 3]
29. copyWithin()
let numbers = [1, 2, 3, 4, 5];
numbers.copyWithin(0, 3);
console.log(numbers); // [4, 5, 3, 4, 5]
30. entries()
let fruits = ['apple', 'banana', 'orange'];
let iterator = fruits.entries();
for (let entry of iterator) {
console.log(entry);
}
// [0, 'apple']
// [1, 'banana']
// [2, 'orange']
31. keys()
let fruits = ['apple', 'banana', 'orange'];
let iterator = fruits.keys();
for (let key of iterator) {
console.log(key);
}
// 0
// 1
// 2
32. values()
let fruits = ['apple', 'banana', 'orange'];
let iterator = fruits.values();
for (let value of iterator) {
console.log(value);
}
// 'apple'
// 'banana'
// 'orange'
Ushbu misollar orqali har bir metodning qanday ishlashini ko’rishingiz mumkin. Keyingi maqolalarda ko’rishuncha.
Ulashish: