• Node.js Video Tutorials

NodeJS URLSearchParams.entries() Method



The NodeJS URLSearchParams.entries() method returns an ES6 Iterator allowing iteration through all the name-value pairs present in the query string.

The iterator returns the name-value pairs in the same order as they present in the query string. Each returned item of the iterator is a JavaScript array. The first item present in the array is the name and the second item is the value, together forming a name-value pair.

The URLSearchParams API provides read and write access to the query of a URL. This class is also available on the global object.

Syntax

Following is the syntax of the NodeJS URLSearchParams.entries() method

URLSearchParams.entries()

Parameters

This method does not accept any parameters.

Return Value

This method returns an ES6 iterator over each of the name-value pairs in the query string.

The below example demonstrates the usage of the Node.js URLSearchParams.entries() method

Example

If the input URL string contains a query segment, the NodeJS urlSearchParams.entries() method will return an iterator over each of the name-value pairs from the query string.

In the following example, we are trying to get the name-value pairs from the input query string.

const url = require('node:url');

let Myurl = new URL('https://www.tutorialspoint.com?1=One&2=Two&3=Three');
console.log("URL: ", Myurl.href);

let params = new URLSearchParams('1=One&2=Two&3=Three');
console.log("Query portion of the URL: " + params.toString());
console.log("The name/value pairs are: ")

for (const [key, value] of params.entries()) {
    console.log(JSON.stringify(`${key}, ${value}`));
}

Output

After executing the above program, the NodeJS entries() method returns the name-value pairs in the input query string.

URL:  https://www.tutorialspoint.com/?1=One&2=Two&3=Three
Query portion of the URL: 1=One&2=Two&3=Three
The name/value pairs are: 
"1, One"
"2, Two"
"3, Three"
nodejs_url_module.htm
Advertisements