How to search an element without using any loops in Node.js ?
Last Updated :
09 Sep, 2020
The setInterval() method repeats or re-schedules the given function at every given time-interval. It is somewhat like window.setInterval() Method of JavaScript API, however, a string of code can’t be passed to get it executed.
Syntax:
setInterval(timerFunction, millisecondsTime);
Parameter: It accepts two parameters which are mentioned above and described below:
- timerFunction <function>: It is the function to be executed.
- millisecondsTime <Time>: It indicates a period of time between each execution.
The clearInterval() method is used to stop the next schedule code execution. It is somewhat like window.clearInterval() Method of JavaScript API, however a string of code can’t be passed to get it executed.
Syntax:
clearInterval(intervalVar);
Parameter: It accepts a single parameter which is mentioned above and described below:
Examples:
Input: Array = [ 46, 55, 2, 100, 0, 500 ]
Search Element = 0
Output: Element 0 found at index 4
Input: Array = [8, 9, 2, 7, 18, 5, 25]
Search Element = 500
Output: Element 500 not found in Array.
Approach: The sorting requires visiting each element and then performing some operations, which requires for loop to visit those elements.
Now here, we can use setInterval() method to visit all those elements and perform those operations.
The below code illustrates the above approach in JavaScript Language.
Example 1: File Name: Index.js
javascript
// Node.js program to search an element
// without using any loops provided Array
const arr = [46, 55, 2, 100, 0, 500];
const l = arr.length;
var j = 0;
// Element to Search
var srchElement = 0;
// setInterval for looping purpose
var myVar1 = setInterval(myTimer1, 1);
function myTimer1() {
if (arr[j] == srchElement) {
// Clear interval as required
// element is found
clearInterval(myVar1);
// Printing found element
console.log("Element", srchElement,
"found at index", arr.indexOf(arr[j]));
}
j++;
if (j == l) {
// Clear interval as element
// not found in array
clearInterval(myVar1);
// Printing that element not found
console.log("Element", srchElement,
"not found in Array.");
}
}
Run index.js file either on the online compiler or follow the following:
node index.js
Output:
Element 0 found at index 4
Example 2: File Name: index.js
javascript
// Node.js program to search an element
// without using any loops provided Array
const arr = [8, 9, 2, 7, 18, 5, 25];
const l = arr.length;
var j = 0;
// Element to Search
var srchElement = 50;
// setInterval for looping purpose
var myVar1 = setInterval(myTimer1, 1);
function myTimer1() {
if (arr[j] == srchElement) {
// Clear interval as required
// element is found
clearInterval(myVar1);
// Printing found element
console.log("Element", srchElement,
"found at index", arr.indexOf(arr[j]));
}
j++;
if (j == l) {
// clear interval as element not
// found in array
clearInterval(myVar1);
// Printing that element not found
console.log("Element", srchElement,
"not found in Array.");
}
}
Run index.js file either on the online compiler or follow the following:
node index.js
Output:
Element 50 not found in Array.
Similar Reads
How to loop through HTML elements without using forEach() loop in JavaScript ?
In this article, we will learn how to loop through HTML elements without using the forEach() method. This can be done in the following ways: Table of Content Approach 1: Using the for loopApproach 2: Using the While loopApproach 3: Using the 'for.....of' statementApproach 4: Using the for...in state
4 min read
How to Make a search function using Node Express and MYSQL
In this article, we will learn how to create a search function using Node with Express framework to search terms inside MYSQL tables. Prerequisites:MySQLNode JSExpress JSWe will discuss the following methods to search: Table of Content Searching term with partial matchSearching term with exact match
5 min read
How to Perform a Find Operation with Limit and Skip in MongoDB using Node.js?
In MongoDB, the find operation is used to query the database and retrieve documents that match a specified criterion. Using limit and skip along with find allows for efficient pagination of results. limit specifies the maximum number of documents to return, while skip specifies the number of documen
3 min read
How to add Search Feature in Next.js using Algolia ?
Adding a search feature to your Next.js application can greatly enhance user experience by providing fast and relevant search results. Algolia is a powerful search-as-a-service solution that integrates seamlessly with Next.js to offer instant, full-text search capabilities. In this article, we will
3 min read
How to Check an Element with Specific ID Exists using JavaScript ?
Given an HTML document containing some elements and the elements contain some id attribute. The task is to check whether the element with a specific ID exists or not using JavaScript. Below are the approaches to check an element with specific ID exists or not using JavaScript:Â Table of ContentApproa
3 min read
How to replace one document in MongoDB using Node.js ?
MongoDB, the most popular NoSQL database, we can count the number of documents in MongoDB Collection using the MongoDB collection.countDocuments() function. The mongodb module is used for connecting the MongoDB database as well as used for manipulating the collections and databases in MongoDB. Inst
1 min read
How to Implement Search and Filtering in a REST API with Node.js and Express.js ?
Search and filtering are very basic features that an API must possess to serve data to the client application efficiently. By handling these operations on the server-side, we can reduce the amount of processing that has to be done on the client application, thereby increasing its performance.In this
5 min read
How To Perform a Find Operation With Sorting In MongoDB Using Node.js?
Performing a find operation with sorting in MongoDB using Node.js is a common task for developers working with databases. This guide will walk you through the process step-by-step, including setting up a MongoDB database, connecting to it using Node.js, performing a find operation, and sorting the r
3 min read
How to Perform Text Search in MongoDB using Node.js?
MongoDB is an open-source, cross-platform, No-SQL database that stores data in documents, which contain data in the form of key-value pairs. In this article, we will learn about how to perform text-based searches in MongoDB using node.js. Prerequisites Node.jsMongoDBMongoDB Atlas Connect with Applic
5 min read
How to Perform a findOne Operation in MongoDB using Node.js?
The findOne operation in MongoDB is used to get a single document from the collection if the given query matches the collection record. While using findOne, if more than one record is there with the exact same match, then it will return the very first one. We will use this operation if we need to fe
4 min read