关联数组的查询参数

问题描述 投票:0回答:1

我想使查询参数成为一个关联数组。

因此,例如:

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');
const queryParams = new URLSearchParams(newURI.search);

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

有人可以帮忙吗?

javascript parameters uri
1个回答
0
投票

使用URLSearchParams.forEach()

URLSearchParams.forEach()
© www.soinside.com 2019 - 2024. All rights reserved.