Please write a program to extract the various JS GET parameters (parameter name and number of uncertainties) URL in, return it to a json structure by key-value form

/**
* Write a function to parse the url of the search parameters, making it a json objects
*/
// idea:? Var url = window.location.search; get the current page url address and the following parameters
// 1. declare an empty object, used to search the url parameter storage
// 2. Strings divided from the question mark array, remove the standard value of 1 is the parameter string url
// 3 The argument string array is divided into &
// 4. Traverse the array, each at a = split, as the key value before the question mark, question mark jsonList value as the value of jsonList, thus generating a target js
// 5. Finally, this object into json objects by JSON.stringify

var url = "https://jobs.51job.com/shenzhen-baq/113561919.html?s=01&t=0"

function searchParams(url) {
was jsonList = {};
if (url.indexOf('?') != -1) {
const search = url.split('?')[1].split('&');
// console.log(search)
for (var i = 0; i < search.length; i++) {
jsonList[search[i].split('=')[0]] = search[i].split('=')[1]
}
}
return JSON.stringify(jsonList)
// console.log(JSON.stringify(jsonList))
}
console.log(searchParams(url));

Guess you like

Origin www.cnblogs.com/guozhuang/p/10963890.html