
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Find Specific Words in an Array with JavaScript
Finding specific words in an array is a common task in JavaScript, especially when you need to filter data, search through lists, or analyze text. An array is a simple and fundamental data structure in JavaScript that stores multiple values of the same data type.
JavaScript has several built-in methods, like includes(), filter(), and find(), that help you search for specific words easily. This article will explain these methods with examples.
Finding Specific Words in an Array
Finding specific words in an array can be done in the following ways:
Using includes() method
The includes() method is one of the best methods for finding a specific value in an array. It returns true if the element exists and false otherwise.
Syntax
The following is the basic syntax of using the includes() method for finding specific words in an array:
array.includes(searchElement, fromIndex);
Let's understand the parameter used in includes() using-regular-expression method:
- searchElement: The element or word that you are searching for in the array.
- fromIndex: The index in the array from which to start searching. If not specified, the search starts from the beginning of the array. This parameter is optional.
Example
The following is a simple example of using includes() method that is finding the specified fruit "kiwi" from an array "fruits" −
const fruits = ["orange", "grape", "kiwi", "mango"]; const searchFruit = "kiwi"; if (fruits.includes(searchFruit)) { console.log(`${searchFruit} found in the array.`); } else { console.log(`${searchFruit} not found in the array.`); }
Output
kiwi found in the array.
Using filter() method
The filter() method is another useful method for finding a specific value in an array. This method finds all occurrences of a specific word or filters an array based on a condition.
Syntax
The following is basic syntax of using filter() method for finding specific words in an array:
const newArray = array.filter((element, index, array) => { // Return true to include the element in the new array return condition; });
Let's understand the parameter used in filter() method:
- element: The current element being processed in the array.
- index (optional): The index of the current element.
- array (optional): The original array being filtered.
Example
The following is a simple example of using filter() method that is finding the specified color "blue" from an array "colors".
const colors = ["red", "blue", "green", "blue", "yellow"]; const searchColor = "blue"; // Find all the times "blue" appears in the array const foundColors = colors.filter(color => color === searchColor); // Show the results console.log(foundColors);
Output
[ 'blue', 'blue' ]
Using find() method
The find() method is another useful method for finding a specific value in an array. The find() method returns the first element that matches a condition.
Syntax
The following is basic syntax of using find() method for finding specific words in an array:
array.find(callback(element, index, array), thisArg);
Let's understand the parameter used in find() method:
- callback: A function that tests each element. It takes the following arguments:
- element: The current element being processed.
- index (optional): The index of the current element.
- array (optional): The array being traversed.
- thisArg (optional): A value to use as this inside the callback.
Example
The following is a simple example of using find() method that is finding the specified animal "cat" from an array "animals".
const animals = ["dog", "cat", "rabbit", "hamster"]; const searchAnimal = "cat"; // Find the first occurrence of "cat" in the array const found = animals.find(animal => animal === searchAnimal); // Show the result console.log(found);
Output
cat
Using Regular Expressions
If you want to look for words that are similar or don't care about uppercase or lowercase letters, you can use regular expressions (RegExp) with the filter() method to help you.
Example
The following is a simple example of using regular expression for finding the specified word "banana" from an array "words".
const words = ["apple", "banana", "cherry", "date"]; const searchWord = "banana"; const found = words.find(word => word === searchWord); console.log(found);
Output
banana
Conclusion
In this article, we have seen multiple methods for finding a specific word from an array. By understanding these methods, you can effectively search and filter words in arrays, making your JavaScript applications more efficient and powerful.