DEV Community

Cover image for Array search methods in JavaScript.!
Hodiev
Hodiev

Posted on • Edited on

Array search methods in JavaScript.!

Javascriptda arraylarning izlash metodlari.!

Array indexOf()
Array lastIndexOf()
Array includes()
Array find()
Array findIndex()
Array findLast()
Array findLastIndex()

1. indexOf()
Ushbu metod massiv elementi joylashgan indeks raqamni qaytaradi.
sintaksis: arrayName.indexOf(item,start);
buyerda item-indeks raqami izlanayotgan element, start-indeks raqami izlanayotgan element qayerdan boshlab izlanishi va startga qiymat berish ihtiyoriy. Element bir necha marotaba takrorlangan bo'lsa birinchi topilgan elementni qaytaradi, agar element topilmasa -1 qaytaradi. Ushbu metod elementlarni chapdan o'ngga qarab izlaydi.!

let fruits = ['lemon','mango','nut','peach','strawberry','nut','apelsin'];
console.log(fruits);
//natija - ['lemon','mango','nut','apelsin','peach','strawberry']

let fruits_indexOf1 = fruits.indexOf('nut');
console.log(fruits_indexOf1);
//natija -   2

let fruits_indexOf2 = fruits.indexOf('nut')+1;
console.log(fruits_indexOf2);
//natija -   3

let fruits_indexOf3 = fruits.indexOf('nut')-4;
console.log(fruits_indexOf3);
//natija -  -2

let fruits_indexOf4 = fruits.indexOf('nut',4);
console.log(fruits_indexOf4);
//natija -   5

let fruits_indexOf5 = fruits.indexOf('nut',6);
console.log(fruits_indexOf5);
//natija -  -1
Enter fullscreen mode Exit fullscreen mode

2. lastIndexOf()
Ushbu metod elementlarni o'ng tarafdan chap tarafga qarab izlaydi va 1-topilgan elementni qaytaradi, sintaksisti indexOf bilan birxil.!

let fruits = ['lemon','mango','nut','peach','strawberry','nut','apelsin'];
console.log(fruits);
//natija - ['lemon','mango','nut','apelsin','peach','strawberry']

let fruits_lastIndexOf1 = fruits.lastIndexOf('nut');
console.log(fruits_lastIndexOf1);
//natija - 5

let fruits_lastIndexOf2 = fruits.lastIndexOf('nut',3);
console.log(fruits_lastIndexOf2);
//natija - 2
Enter fullscreen mode Exit fullscreen mode

3. includes()
Ushbu metod massiv ichidan biror-bir element bor yoki yo'qligini bilish uchun foydalaniladi, agar element bor bo'lsa true bo'lmasa false qiymat qaytaradi.!
sintsksis: `arrayName.includes(item,start);`
item-mos element, start-izlash qayerdan boshlanishi kerakligi. includes() ECMAScript 2016 da kiritilgan.!

let fruits = ['lemon','mango','nut','peach','strawberry','nut','apelsin'];
console.log(fruits);
//natija - ['lemon','mango','nut','apelsin','peach','strawberry']

let fruits_includes1 = fruits.includes('lemon');
console.log(fruits_includes1);
//natija - trtue

let fruits_includes2 = fruits.includes('lemon',3);
console.log(fruits_includes2);
//natija - false

let fruits_includes3 = fruits.includes();
console.log(fruits_includes3);
//natija - false
Enter fullscreen mode Exit fullscreen mode

4. find()

5. findIndex()

6. findLast()

7. findLastIndex()

Top comments (0)