URLSearchParams values & keys() in Node

Node.jsServer Side ProgrammingProgramming

Introduction to values()

This function returns an iterator that allows us to iterate all over the values that are present in that object. It basically gives us a tool to select or iterate the values and then perform functions on them.

Syntax

URLSearchParams.values();

It will return an ES6 type iterator with the name-value pair over all the values.

Example

// Defining the parameters as a constant
var params = new URLSearchParams(
'key1=value1&key2=value2&key3=value3');

// Iterating over the values of params
for(var value of params.values()) {
   console.log(value);
}

Output

value1
value2
value3

Example

// Defining the URL as a constant
const params = new URLSearchParams('name=John&age=21');

// Iterating over the values of params
for(var value of params.values()) {
   console.log(value);
}

Output

John
21

Introduction to keys()

This function returns an iterator that allows us to iterate all over the keys that are present in that object. It basically gives us a tool to select or iterate the values and then perform functions on them. It is similar to values. The only difference is values iterate over the values, and keys are used for iterating over the keys.

Syntax

URLSearchParams.keys();

It will return an ES6 type iterator with the name-value pair over all the keys.

Example

// Defining the parameters as a constant
var params = new URLSearchParams(
'key1=value1&key2=value2&key3=value3');

// Iterating over the values of params
for(var key of params.keys()) {
   console.log(key);
}

Output

key1
key2
key3

Example

// Defining the URL as a constant
const params = new URLSearchParams(
'name=John&age=21');

// Iterating over the values of params
for(var key of params.keys()) {
   console.log(key);
}

Output

name
age
raja
Published on 28-Apr-2021 07:26:04
Advertisements