javascript - 如何从 url 查询字符串,javascript 中获取关联数组
问题描述
我有 url 查询字符串
?filter%5B%22customer_rating_heading%22%5D=customer_rating_6_up%2Ccustomer_rating_7_up%2Ccustomer_rating_9_up&map=1
这是
?filter["customer_rating_heading"]=customer_rating_6_up,customer_rating_7_up,customer_rating_9_up&map=1
我想要求filter
喜欢params.get('filter)
并得到
[customer_rating_heading => 'customer_rating_6_up,customer_rating_7_up,customer_rating_9_up' //(later I'll make array from this string)
但我不知道该怎么做,我试过let query = Object.fromEntries(new URLSearchParams(location.search)
但它给了我:
{filter["customer_rating_heading"]: "customer_rating_6_up,customer_rating_7_up,customer_rating_9_up"
map: "1"}
但我无法得到过滤器,就像query.filter
它总是未定义一样
有人知道此类问题的解决方案吗?
解决方案
用“匹配”解决
let query = Object.fromEntries(new URLSearchParams(location.search))
let firstKey = Object.keys(query)[0];
let found = firstKey.match(/\[(.*)\]/)[1]
let key = found.replace(/\"/g, '');
let value = query[firstKey];
console.log(`${key} => ${value}`)
推荐阅读
- mongodb - 在前端构造 mongodb 查询是不是一个坏主意?
- java - FileInputStream 抛出 FileNotFoundException
- python - python unittest - 如何保留已转换为 Mock 对象的函数的副作用?
- machine-learning - AutoEncoder 和 IsolationForest 的 ROC AUC 分数
- networking - 使用 ns3 的基于 tcp 的客户端-服务器系统
- vba - 按访问表单上的多个参数过滤
- javascript - 为什么我们需要将道具传递给孩子?
- macos - Docker Desktop for Mac OSCatalina 在容器中挂载 SD 卡卷作为循环设备以使用 dd 闪存 SD 卡
- parse-platform - 当我在单行上运行查询时解析服务器崩溃
- c# - 我的表单冻结以供第二和执行的问题