返回包含当前URL参数的对象。
通过适当的正则表达式,使用 match()
来获得所有的键值对, Array.reduce()
来映射和组合成一个单一的对象。
将 location.search
作为参数传递给当前 url
。
const getURLParameters = url => url .match(/([^?=&]+)(=([^&]*))/g) .reduce((a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), {});
getURLParameters('http://url.com/page?name=Adam&surname=Smith'); // {name: 'Adam', surname: 'Smith'}
更多代码 JavaScript 实用代码片段 请查看 https://www.html.cn/30-seconds-of-code/
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂