题解 | #参数解析器#
参数解析器
https://www.nowcoder.com/practice/9a4cca8c0779438a8be39d45d0370597
// html 从url拿到参数,并转化为对象1 const _getParams = (url = window.location.href) => { let obj = {} url.replace(/([\w-_\u4e00-\u9fa5]+)=([\w-_\u4e00-\u9fa5]+)/g, function(a, b, c) { obj[b] = c; }) return obj; }
// html 从url拿到参数,并转化为对象2 let urlToJsonOpts = (url = window.location.href) => { // 箭头函数默认传值为当前页面url let obj = {}, index = url.indexOf('?'), // 看url有没有参数 params = url.substr(index + 1); // 截取url参数部分 id = 1 & type = 2 if(index != -1) { // 有参数时 let parr = params.split('&'); // 将参数分割成数组 ["id = 1 ", " type = 2"] for(let i of parr) { // 遍历数组 let arr = i.split('='); obj[arr[0]] = arr[1]; } } return obj; }
从url拿到参数,并转化为对象
#JavaScript#
在线编程题解 文章被收录于专栏
web技术
