DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

Array Search methods

indexOf :

  • indexOf() is used to find the index of an element.
  • It return -1 if the element is not found.
  • If the item is present more than once, it returns the position of the first occurrence.
  • Case Sensitive: "A" and "a" are considered different values.

array.indexOf(item, start)

const fruits = ["Apple","Orange","Banana","Papaya","Guava"]

for(let a of fruits){
    document.write("Index of "+ a+ " : "+fruits.indexOf(a))
    document.write("<br>")
}
document.write(fruits.indexOf("Onion"))


Output : 
Index of Apple : 0
Index of Orange : 1
Index of Banana : 2
Index of Papaya : 3
Index of Guava : 4

-1

Enter fullscreen mode Exit fullscreen mode
const numberz = [1,2,3,4,4,2]

document.write("Index of 2 :"+numberz.indexOf(2)) 
document.write("<br>")
document.write("Index of 4 :"+numberz.indexOf(4))
document.write("<br>")

document.write("Index of 2 :"+numberz.indexOf(2,2))
document.write("<br>")
document.write("Index of 4 :"+numberz.indexOf(4,2))
document.write("<br>")

Output : 
Index of 2 :1
Index of 4 :3
Index of 2 :5
Index of 4 :3
Enter fullscreen mode Exit fullscreen mode

lastIndexOf() :

  • It returns the position of the last occurrence of the specified element.
  • It searches from right → left.
  • when value for the start index is given, it starts from that index and looks backward.

array.lastIndexOf(item, start)

const numberz = [1,2,3,4,4,2]

document.write("Index of 2 :"+numberz.lastIndexOf(2)) 
document.write("<br>")
document.write("Index of 4 :"+numberz.lastIndexOf(4))
document.write("<br>")

document.write("Index of 2 :"+numberz.lastIndexOf(2,2))
document.write("<br>")
document.write("Index of 4 :"+numberz.lastIndexOf(4,2))
document.write("<br>")

Output : 
Index of 2 :5
Index of 4 :4
Index of 2 :1
Index of 4 :-1

Enter fullscreen mode Exit fullscreen mode

includes() :

  • It is used to determine whether an array contains a specific value among its entries. It returns a boolean value true if the value is found, and false otherwise.
const fruits = ["Apple","Orange","Banana","Papaya","Guava"]
document.write(fruits.includes("Orange"))
document.write("<br>")
document.write(fruits.includes("orange"))
document.write("<br>")
document.write(fruits.includes("orange",2))

Output :
true
false
false
Enter fullscreen mode Exit fullscreen mode

find() :

  • It is used to get the first element that matches a condition.
  • It returns the element, if found, otherwise undefined.
  • It checks each element one by one.
  • It stops and returns the element, as soon as condition is true.
const fruits = ["Apple","Orange","Banana","Papaya","Guava"]
document.write(fruits.find(a=> a==="Banana" ))
document.write("<br>")

const numberz = [1,2,4,7]
document.write(numberz.find(a=> a%2 == 0))
document.write("<br>")
document.write(numberz.find(a=> a%3 == 0))

Output :
Banana
2
undefined
Enter fullscreen mode Exit fullscreen mode

findIndex() :

  • It is used to find the index of the first element that matches a condition.
  • It returns index, if the condition is matched, -1 otherwise.
const fruits = ["Apple","Orange","Banana","Papaya","Guava"]

document.write(fruits.find(a=> a==="Banana" ))
document.write("<br>")

const numberz = [1,2,4,7]
document.write(numberz.findIndex(a=> a%2 == 0))
document.write("<br>")
document.write(numberz.findIndex(a=> a%3 == 0))

Output :
Banana
1
-1
Enter fullscreen mode Exit fullscreen mode
let users = [
  { id: 1, name: "Arun" },
  { id: 2, name: "Varun" },
  { id: 3, name: "Tharun" }
];

let index = users.findIndex(u => u.id === 2);

console.log(index);

Output :
1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)