Open In App

Node.js urlSearchParams.keys() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The urlSearchParams.keys() method is an inbuilt application programming interface of the URLSearchParams class within url module which is used to get the iterator object containing all the name entries only of URL search params object.

Syntax:

const urlSearchParams.keys()

Parameter: This method does not accept any parameter.

Return value: This method returns the iterator object containing all the name entries only of URL search params object.

Below programs illustrates the use of urlSearchParams.keys() method in Node.js:

Example 1: Filename: app.js




// Node.js program to demonstrate the 
// URLSearchParams.keys() method
   
// Importing the 'url' module
const http = require('url');
  
// Creating and initializing 
// URLSearchParams object
const params = new URLSearchParams();
  
// Appending value in the object
params.append('A', 'Book');
params.append('B', 'Pen');
params.append('C', 'Pencile');
  
// Getting all the name entries only
// by using keys() API
const iterator = params.keys();
  
// Display result for each name entry
console.log("list of all the keys");
for (const [name] of iterator) {
    console.log(name);
}


Run app.js file using the following command:

node app.js

Output:

list of all the keys
A
B
C

Example 2:
Filename: app.js




// Node.js program to demonstrate the 
// URLSearchParams.keys() method
   
// Importing the module 'url'
const http = require('url');
  
// Creating and initializing
// URLSearchParams object
const params = new URLSearchParams();
  
// Appending value in the object
params.append('G', '1');
params.append('F', '2');
params.append('G', '3');
  
// Getting all the name entries only
// by using keys() api
const iterator = params.keys();
  
// Display result for each name entry
console.log("list of all the keys");
for (const [name] of iterator) {
    console.log(name);
}


Run app.js file using the following command:

node app.js

Output:

list of all the keys
G
F
G

Note: The above program will not run on online JavaScript and script editor.

Reference: https://nodejs.org/dist/latest-v14.x/docs/api/url.html#url_urlsearchparams_keys
 



Last Updated : 07 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads