

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Searching an element in Javascript Array
Javascript provides a collection of functions that you can use to find elements in an array. Let's start with the most basic one. The indexOf function goes through the entire array and returns the index of the element you searched for, if it is found else it returns -1. For example,
Example
let people = ["Harry", "Martha", "John", "Sam"]; console.log(people.indexOf("John")) console.log(people.indexOf("Jim"))
Output
This will give the output −
2 -1
There are other, more complex functions that you can use to make search more powerful. Let's look at find() method. The find() method returns the first object matching the condition you provide it as the callback() method. For example,
Example
let people = [{ name: 'Agnes', age: 25 }, { name: 'Richard', age: 21 }, { name: 'Zoe', age: 35 }]; let personNameStartsWithR = people.find(person => person.name[0] === 'R'); console.log(personNameStartsWithR)
Output
This will give the output −
{ name: 'Richard', age: 21 }
But above result gives us an object. We can find the index of this object using the findIndex function. For example,
Example
let people = [{ name: 'Agnes', age: 25 }, { name: 'Richard', age: 21 }, { name: 'Zoe', age: 35 }]; let personNameStartsWithR = people.findIndex(person => person.name[0] === 'R'); console.log(personNameStartsWithR)
Output
This will give the output −
1
Note that the find() and findindex() functions take the callback as an argument and the callback takes arguments: element, index, array. These functions only give the first occurrence of the element. The indexOf function also takes another parameter, fromIndex, so that you can continue search from that point onwards. For example,
Example
let people = ["Harry", "Martha", "John", "Sam", "Martha"]; console.log(people.indexOf("Martha")); console.log(people.indexOf("Martha", 3))
Output
This will give the output −
1 4
- Related Questions & Answers
- Adding an element in an array using Javascript
- Removing an element from an Array in Javascript
- Searching in a sorted 2-D array in JavaScript
- Searching for values in an Javascript Binary Search Tree
- Finding the majority element of an array JavaScript
- How to search for an element in JavaScript array?
- Inserting element at falsy index in an array - JavaScript
- Finding the first redundant element in an array - JavaScript
- Searching for target string in a strangely sorted array in JavaScript
- Finding missing element in an array of numbers in JavaScript
- How to validate if an element in an array is repeated? - JavaScript
- Searching objects by value in JavaScript
- JavaScript code to print last element of an array
- An element inside another element in JavaScript?
- Constructing an array of addition/subtractions relative to first array element in JavaScript