例如:传参{name:'张三',age:18},
function urlFn(obj) { let url = 'http://www.alibb.com' let keys = Object.keys(obj) let values = Object.values(obj) url += '?' for (let i = 0; i < keys.length; i++) { if (i == keys.length - 1) { url += `${keys[i]}=${values[i]}` } else { url += `${keys[i]}=${values[i]}&` } } return url }
得到http://www.alibb.com?name=张三&age=18
标签:www,obj,url,values,keys,参数,let,解析 From: https://blog.csdn.net/Ljt99/article/details/140443520url 不固定可以采用传参的方式
function urlFn(obj,url) { let keys = Object.keys(obj) let values = Object.values(obj) url += '?' for (let i = 0; i < keys.length; i++) { if (i == keys.length - 1) { url += `${keys[i]}=${values[i]}` } else { url += `${keys[i]}=${values[i]}&` } } return url } let obj = { name:'小明', age:'18' } let url = 'http://www.alibb.com' console.log(urlFn(obj,url)) //http://www.alibb.com?name=小明&age=18