Node 中的 URLSearchParams entries 和 forEach
entries() 简介 −
此函数返回一个迭代器,允许我们迭代对象中存在的所有条目集合。它基本上为我们提供了一种迭代查询对象的完整条目集合的工具。
语法
URLSearchParams.entries();
它将返回一个 ES6 类型迭代器,其中包含所有名称-值对值。
示例
// 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]);
}输出
key1 -> value1 key2 -> value2 key3 -> value3
示例
// 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]);
}输出
name -> John age -> 21
forEach(fn[,arg]) 简介
forEach 下描述的 fn 将调用每个名称-值对,这些对将遍历此 forEach 循环,arg 是在调用“fn”时使用的对象。它将在查询中的每个名称-值对上被调用并调用函数。
语法
URLSearchParams.forEach();
它将返回一个 ES6 类型迭代器,其中包含所有键上的名称-值对。
示例
// 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);
});输出
name John age 21
示例
// 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);
});输出
key1 value1 true key2 value2 true key3 value3 true
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP