Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
URLSearchParams entries & forEach in Node
Introduction to entries() −
This function returns an iterator that allows us to iterate all over the entry set that are present in the object. It basically gives us a tool to iterate over the complete entry set of the param object.
Syntax
URLSearchParams.entries();
It will return an ES6 type iterator with all the name-value pair values.
Example
// Defining the parameters as a variable
var params = new URLSearchParams('key1=value1&key2=value2&key3=value3');
// Iterating over the values of params
for(var entry of params.entries()) {
console.log(entry[0] + ' -> ' + entry[1]);
}
Output
key1 -> value1 key2 -> value2 key3 -> value3
Example
// Defining the URL as a constant
const params = new URLSearchParams(
'name=John&age=21');
// Iterating over the values of params
for(var entry of params.entries()) {
console.log(entry[0] + ' -> ' + entry[1]);
}
Output
name -> John age -> 21
Introduction to forEach(fn[,arg])
The fn described under the forEach will be invoked each and every name-value pair that will iterated through this forEach loop and arg is an object that will be used when 'fn' is called. It will be called over each name-value pair in the query and invokes the function.
Syntax
URLSearchParams.forEach();
It will return an ES6 type iterator with the name-value pair over all the keys.
Example
// Defining the URL as a constant
const url = new URL(
'https://example.com/name=John&age=21');
// Iterating over the values of params
url.searchParams.forEach(function(value,key) {
console.log(value, key);
});
Output
name John age 21
Example
// Defining the parameters as a constant
const myURL = new URL(
'https://example.com/key1=value1&key2=value2&key3=value3');
// Iterating over the values of params
myURL.searchParams.forEach(
(value, name, searchParams) => {
console.log(name, value,
myURL.searchParams === searchParams);
});
Output
key1 value1 true key2 value2 true key3 value3 true
Advertisements