DEV Community

Cover image for Array methods in JavaScript.!
Hodiev
Hodiev

Posted on • Edited on

Array methods in JavaScript.!

JavaScriptda Array metodlari.!!!

Array length
Array toString()
Array at()
Array join()
Array pop()
Array push()
Array shift()
Array unshift()
Array delete()
Array concat()
Array copyWithin()
Array flat()
Array splice()
Array toSpliced()
Array slice()

1. length
Ushbu metod Array uzunligini qaytaradi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

let codingLang_length = codingLang.length;
console.log(codingLang_length);
//natija - 8
Enter fullscreen mode Exit fullscreen mode

2. toString()
Ushbu metod Array elementlarini satr ko'rinishdagi qatorga o'giribberadi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_toString = codingLang.toString();
console.log(codingLang_toString);
//natija - JavaScript,Go,PhP,Python,C,C++,Java,Kotlin
Enter fullscreen mode Exit fullscreen mode

3. at()
Ushbu metod at() qavslari ichiga yozilgan indeks raqamidagi Array elementini qaytaradi, agar raqamga mos indeks(element) mavjud bo'lmasa undefindedqaytaradi, shu bilan birga ushbu metodda manfiy qiymat qo'llashimizham mumkin va manfiy qiymat qo'llaganimizda mos elementni o'ngdan chapga qarab izlaydi !
at() metodi ES2022 da kiritilgan.

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_at1 = codingLang.at(2);
console.log(codingLang_at1);
//natija - PhP 

const codingLang_at2 = codingLang.at(42);
console.log(codingLang_at2);
//natija - undefined

const codingLang_at3 = codingLang.at(-2);
console.log(codingLang_at3);
//natija - Java
Enter fullscreen mode Exit fullscreen mode

4. join()
Ushbu metod toString() kabi Array elementlarini vergul bilan ajratilgan satr ko'rinishga o'giribberadi, ammo toString() dan farq qilgan jihati shuki: join() qavslari ichida yozgan elementimiz har-bir Array elementlari orasiga tushgan xolda satr ko'rinisha o'tadi hechnarsa yozmasak toString kasi ishlaydi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_join1 = codingLang.join();
console.log(codingLang_join1);
//natija - JavaScript,Go,PhP,Python,C,C++,Java,Kotlin

const codingLang_join2 = codingLang.join(' ');
console.log(codingLang_join2);
//natija - JavaScript Go PhP Python C C++ Java Kotlin

const codingLang_join3 = codingLang.join(' and ');
console.log(codingLang_join3);
//natija - JavaScript and Go and PhP and Python and C and C++ and Java and Kotlin
Enter fullscreen mode Exit fullscreen mode

5. pop()
Ushbu metod Array elementlari oxiridan bitta elementni kesiboladi, asl Arrayga ta'sir qiladi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_pop = codingLang.pop();
console.log(codingLang_pop);
//natija - kotlin

console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java']
Enter fullscreen mode Exit fullscreen mode

6. push()
Ushbu metod Arrayga yangi element qo'shish uchun ishlatiladi, qo'shilgan yangi metod asl Arrayda seziladi metod ishlatilgan qator yangi Array usunligini qaytaradi. Quyidagi misolda yanada tushunarliroq.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_push = codingLang.push('React');
console.log(codingLang_push);
//natija - 9

console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin', 'React']
Enter fullscreen mode Exit fullscreen mode

7. shift()
Ushbu metod Array boshidagi ya'ni indeks raqami 0 bo'lgan elementni kesib oladi va qaytaradi, asl Arrayga ta'sir qiladi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const  codingLang_shift = codingLang.shift();
console.log(codingLang_shift);
//natija -  JavaScript

console.log(codingLang);
//natija - ['Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

8. unshift()

Ushbu metod Array boshidan yangi element qo'shadi va asl Arrayga tasir qiladi, metod ishlatilgan qator yangi Array uzunligini qaytaradi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_unShift = codingLang.unshift('laravel');
console.log(codingLang_unShift);
//natija - 9

console.log(codingLang);
//natija - ['laravel', 'JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

9. delete()
Ushbu metod Array elementini o'chirish uchin xizmat qiladi, o'chirilgan massiv o'rni empty bo'sh xolga keladi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_delete = codingLang.delete;
console.log(delete codingLang[2]);
//natija - 

console.log(codingLang);
//natij - ['JavaScript', 'Go', empty, 'Python', 'C', 'C++', 'Java', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

10. concat()
Ushbu metod Arraylarni birlashtirish uchun ishlatiladi.!

const codingLang_1 = ['JavaScript','Go','PhP'];
console.log(codingLang_1);
//natija -  ['JavaScript', 'Go', 'PhP']

const codingLang_2 = ['Python','C','C++','Java','Kotlin'];
console.log(codingLang_2);
//natija - ['Python', 'C', 'C++', 'Java', 'Kotlin'

const codingLang_concat = codingLang_1.concat(codingLang_2);
console.log(codingLang_concat);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

11. copyWithin()
Ushbu metod Array elementlarini Arraydagi boshqa joyga ko'chiradi.!

ushbu misolda ko'rinibturibdiki: birinchi element nolinchi element o'rniga nusxalanmoqda.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_copyWithin1 = codingLang.copyWithin(0,1);
console.log(codingLang_copyWithin1);
//natija - ['Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin', 'Kotlin']

const codingLang_copyWithin2 = codingLang.copyWithin(2,0,2);
console.log(codingLang_copyWithin2);
//natija - ['Go', 'PhP', 'Go', 'PhP', 'C++', 'Java', 'Kotlin', 'Kotlin']


console.log(codingLang);
//natija - ['Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

12. flat()
Ushbu metod ichma-ich kelgan Arraylarni bitta Array ichida tekislash, joylashtirish uchun ishlatiladi.!
ES2019 da kiritilgan.!

const mixArray = [['css','html','sass'], [1,2,3,4], ['object','array','number']];
console.log(mixArray);
//natija - (3) [Array(3), Array(4), Array(3)]

const codingLang_flat = mixArray.flat();
console.log(codingLang_flat);
//natija - ['css', 'html', 'sass', 1, 2, 3, 4, 'object', 'array', 'number']

console.log(mixArray);
//natija - (3) [Array(3), Array(4), Array(3)]
Enter fullscreen mode Exit fullscreen mode

13. splice()
Ushbu metod Arrayga yangi element qo'shish uchun, element o'chirish uchun ishlatiladi.!
Sintaksis:arrayName.splice(a,b,newElement1,newElement2);
Buyerda: a-yangi element qo'shilishi kerak bo'lgan joyni belgilaydi b-qancha element olibtashlanishi kerakligini belgilayydi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_splice1 = codingLang.splice(0,1,'angular');
console.log(codingLang_splice1);
//natija - ['JavaScript']

console.log(codingLang);
//natija - ['angular', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

14. toSpliced()
Ushbu metod ES2023 da qo'shilgan bo'lib slice() dan farq qilgan xolda asl Arrayga ta'sir qilmagan xolda ishlaydi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_toSpliced = codingLang.toSpliced(0, 1);
console.log(codingLang_toSpliced);
//natija - ['Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']


console.log(codingLang);
//natija - ['angular', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

15. slice()
Ushbu metod Arrayning bir qismini boshqa array holatda qaytaradi.
sintaksis: Array.slice(a,b);
bu yerda a-qayerdan boshlanishi b-qayerda tugashi.!

const codingLang = ['JavaScript','Go','PhP','Python','C','C++','Java','Kotlin'];
console.log(codingLang);
//natija - ['JavaScript', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']

const codingLang_slice = codingLang.slice(2,4);
console.log(codingLang_slice);
//natija - ['PhP', 'Python']


console.log(codingLang);
//natija - ['angular', 'Go', 'PhP', 'Python', 'C', 'C++', 'Java', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)