NodeJS urlSearchParams[Symbol.iterator]() Method
The NodeJS URLSearchParams[Symbol.iterator]() method returns an ES6 Iterator allowing iteration through all the name-value pairs present in the query string. This method is an alias for URLSearchParams.entries() method.
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[Symbol.iterator]() method
URLSearchParams[Symbol.iterator]()
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.
Example
If the input URL string contains a query segment, the NodeJS URLSearchParams[Symbol.iterator]() 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?10=Ten&20=Twenty&30=Thirty&40=Fourty');
console.log("URL: ", Myurl.href);
let params = new URLSearchParams('10=Ten&20=Twenty&30=Thirty&40=Fourty');
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
On executing the above program, it will generate the following output
URL: https://www.tutorialspoint.com/?10=Ten&20=Twenty&30=Thirty&40=Fourty Query portion of the URL: 10=Ten&20=Twenty&30=Thirty&40=Fourty The name/value pairs are: "10, Ten" "20, Twenty" "30, Thirty" "40, Fourty"