- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 all occurrences of a word in array in JavaScript
We are required to write a JavaScript function that takes in an array of literals as the first argument and a string as the second argument. Our function should return the count of the number of times that string (provided by second argument) appears anywhere in the array.
Example
The code for this will be −
const arr = ["word", "a word", "another word"]; const query = "word"; const findAll = (arr, query) => { let count = 0; count = arr.filter(el => { return el.indexOf(query) != -1; }).length; return count; }; console.log(findAll(arr, query));
Output
And the output in the console will be −
3
- Related Articles
- How to replace all occurrences of a word in a string with another word in java?
- Remove all occurrences of a multiple occurring element in an array in JavaScript
- How to replace all occurrences of a string in JavaScript?
- C# program to count occurrences of a word in string
- Java program to count occurrences of a word in string
- Iterating through an array, adding occurrences of a true in JavaScript
- Python program to count occurrences of a word in a string
- Unique number of occurrences of elements in an array in JavaScript
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- Write a python program to count occurrences of a word in string?
- Finding all valid word squares in JavaScript
- Match all occurrences of a regex in Java
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- How to count number of occurrences of repeated names in an array - JavaScript?
- How to find the sum of all elements of a given array in JavaScript?

Advertisements