lg_frontend/utils/query.js

28 lines
740 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @function 将url中的查询字符串转换为obj
* @param search { String } 查询字符串默认为当前url中的查询字符串
* @return { Object }
*/
export const query2object = (search = location.search.substr(1)) => {
if (!search.length) return {}
const query = {}
for (const queryString of search.split('&')) {
const kv = queryString.split('=')
query[kv[0]] = kv[1]
}
return query
}
/**
* @function 将对象转换为查询字符串,不带#和?
* @param object
*/
export const object2query = (object) => {
if (Object.keys(object).length === 0) return ''
let search = ''
for (const key in object) {
search += `${key}=${object[key]}&`
}
return search.substr(0, search.length - 1)
}