Array methods in JavaScript.!
Published: 2024-06-29 18:57:29

**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()
`

<u>1. `length`</u>
UShbu metod massivning 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
```



<u>2. `toString()`</u>
Ushbu metod massiv elementlarini string 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
```


<u>3. `at()`</u>
Ushbu metod  at() qavslari ichiga yozilgan  indeks raqamidagi massiv elementini qaytaradi, agar raqamga mos indeks(element) mavjud bo'lmasa `undefinded `qaytaradi, 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
```


<u>4. `join()`</u>
Ushbu metod `toString()` kabi massiv elementlarini vergul bilan ajratilgan satr ko'rinishga o'giribberadi, ammo `toString()` dan farq qilgan jihati shuki: `join()` qavslari ichida yozgan elementimiz har-bir massiv 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
```


<u>5. `pop()`</u>
Ushbu metod massiv elementlari oxiridan bitta elementni kesiboladi, asl massivga 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']
```





<u>6. `push()`</u>
Ushbu metod massivga yangi element qo'shish uchun ishlatiladi, qo'shilgan yangi metod asl massivda seziladi metod ishlatilgan qator yangi massiv 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']
```




<u>7. `shift()`</u>
Ushbu metod massiv boshidagi ya'ni indeks raqami 0 bo'lgan elementni kesib oladi va qaytaradi, asl massivga 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']
```



<u>8. `unshift()`</u>

Ushbu metod massiv boshidan yangi element qo'shadi va asl massivga tasir qiladi, metod ishlatilgan qator yangi massiv 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']
```


<u>9. `delete()`</u>
Ushbu metod massiv 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']
```


<u>10. `concat()`</u>
Ushbu metod massivlarni 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']
```



<u>11. `copyWithin()`</u>
Ushbu metod massivdagi elementlarini massivdagi boshqa joyga ko'chiradi.!

<u>ushbu misolda ko'rinibturibdiki:</u> 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']
```


<u>12. `flat()`</u>
Ushbu metod ichma-ich kelgan massivlarni bitta massiv 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)]
```



<u>13. `splice()`</u>
Ushbu metod massivga yangi element qo'shish uchun ishlatiladi.!
<u>**Sintaksis:**`arrayName.splice(a,b,newElement1,newElement2);`</u>
Buyerda: **a**-yangi element qo'shilishi kerak bo'lgan joyni belgilaydi **b**-qancha element olibtashlanishi kerakligini belgilayydi.!

<u>14. `toSpliced()`</u>


<u>15. `slice()`</u>.