首页 > 解决方案 > 查询参数到关联数组

问题描述

我想将查询参数设置为关联数组。

因此,例如:

http://localhost:8000/products?&size=2&type=all&nice&categories=a%20%b%20&m=true

应该:

{
  size: '2',
  type: 'all',
  nice: undefined,
  categories: ['a', 'b', 'c'],
  m: 'true'
}

我的做法:

const newURI = new URL('http://localhost:8000/products?&size=2&type=all&nice&categories=a%20%b%20&m=true');
const queryParams = new URLSearchParams(newURI.search);

const associativeObj = { ...Array.from(queryParams).map(el => {
    el[0]: el[1]
  })
}

有人可以帮忙吗?

标签: javascriptparametersuri

解决方案


推荐阅读