对象转换成查询字符串
解析完毕的对象 { name: 'jack', age: 18, gender: '男', classRoom: 2109 }
// 对象转换成查询字符串
let queryStringify = obj => {
let str = ''
for (var k in obj) str += k + '=' + obj[k] + '&'
return str.slice(0, -1)
}
let res = queryStringify(obj)
console.log(res)
查询字符串解析成对象
// 查询字符串解析成对象
let parseString = (s, k) => {
let obj = {}
s.split('&').forEach(item => {
let t = item.split('=')
obj[t[0]] = t[1]
})
return k ? obj[k] : obj
}
let res = parseString(str)
let res = parseString(str, '想要的key值')
console.log(res)