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 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 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 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
How To Query For Documents In MongoDB Using NodeJS? MongoDB is the popular NoSQL database that allows for flexible and scalable data storage. NodeJS and JavaScript runtime built on Chrome's V8 JavaScript engine. It is often used with MongoDB to build powerful and efficient applications. In this article, we will guide you on how to query the documents
4 min read