Node.js urlSearchParams.keys() Method
Last Updated :
07 Oct, 2021
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
const http = require( 'url' );
const params = new URLSearchParams();
params.append( 'A' , 'Book' );
params.append( 'B' , 'Pen' );
params.append( 'C' , 'Pencile' );
const iterator = params.keys();
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
const http = require( 'url' );
const params = new URLSearchParams();
params.append( 'G' , '1' );
params.append( 'F' , '2' );
params.append( 'G' , '3' );
const iterator = params.keys();
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...