📜  decoDe 查询字符串到对象 javascript - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:30.978000             🧑  作者: Mango

代码示例1
type mapping = {
  [key: string]: string
}

function url_to_object(url: string): mapping {
  const data: {[key: string]: any} = {}
  url
    .replace(/\/\?/, '')
    .split('&')
    .map(x => x.split('='))
    .forEach(x => {
      data[x[0]] = x[1]
    })
  return data;
}

url_to_object('id=4&second=3')    // {id: 4, second: 3}