获取地址栏参数转成对象

export const quertObject = (url) => {
  const queryString = url.split('?')[1]
  const obj = {}
  const arr = queryString.split('&')
  for (let i = 0; i < arr.length; i++) {
    const key = arr[i].split('=')[0]
    const value = arr[i].split('=')[1]
    obj[key] = value
  }
  return obj
}
export const getQueryParams = () => {
  const result = {}
  const querystring = window.location.search
  const reg = /[?&][^?&]+=[^?&]+/g
  const found = querystring.match(reg)
  if (found) {
    found.forEach((item) => {
      let temp = item.substring(1).split('=')
      let key = temp[0]
      let value = temp[1]
      result[key] = value
    })
  }
  return result
}

 

posted @ 2021-08-25 23:38  吴小明-  阅读(105)  评论(0编辑  收藏  举报