new URLSearchParams () usage instructions

URLSearchParams The interface defines some practical methods to process the query string of the URL.

method:

This interface does not inherit any attributes.

URLSearchParams.append()

 Insert a specified key / value pair as a new search parameter.

URLSearchParams.delete()

 Delete the specified search parameter and its corresponding value from the search parameter list.

URLSearchParams.entries()

 Returns an iteratorobject that can traverse all key / value pairs.

URLSearchParams.get()

 Get the first value of the specified search parameter.

URLSearchParams.getAll()

 Get all the values ​​of the specified search parameters, the return is an array.

URLSearchParams.has()

 Returns  Boolean whether the search parameter exists.

URLSearchParams.keys()

Returns iterator all the key names of this object containing key / value pairs.

URLSearchParams.set()

 Set a new value for the search parameter. If there are multiple values, all other values ​​will be deleted.

URLSearchParams.sort()

 Sort by key name.

URLSearchParams.toString()

 Returns a string composed of search parameters, which can be used directly on the URL.

URLSearchParams.values()

 Returns iterator all values ​​of this object containing key / value pairs.

Examples

var paramsString = "q=URLUtils.searchParams&topic=api"
var searchParams = new URLSearchParams(paramsString);

for (let p of searchParams) {
  console.log(p); // [q, URLUtils.searchParams]、[topic, api]
}

searchParams.has("topic") === true; // true
searchParams.get("topic") === "api"; // true
searchParams.getAll("topic"); // ["api"]
searchParams.get("foo") === ""; // true
searchParams.append("topic", "webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev"
searchParams.set("topic", "More webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev"
searchParams.delete("topic");
searchParams.toString(); // "q=URLUtils.searchParams"

 

Published 14 original articles · Like 4 · Visitors 8816

Guess you like

Origin blog.csdn.net/weixin_41575159/article/details/93169070