当前位置:网站首页>How to convert url to obj or obj to url
How to convert url to obj or obj to url
2022-08-11 00:51:00 【piano~~】
url转成obj或者obj转成url的方法
1、url转成obj
function urlToObj(url) {
if (typeof url !== 'string') return 'Please pass in the correct parameter type'
const urlArr = url.split('?');
let obj = {
};
let params = {
};
if (urlArr?.length) {
urlArr.forEach((item, index) => {
if (!index) return obj.path = item;
const itemArr = item?.split('&');
itemArr.forEach(it => {
if (it) {
const itArr = it.split('=');
if (itArr[0]) params[itArr[0]] = itArr[1];
}
})
})
}
obj.query = params;
return obj
}
使用:
const a = urlToObj('http:baidu.com?age=1&sex=nan')
console.log(a);//{path: 'http:baidu.com', query: {…}}
2、obj转成url
function objToUrl(url) {
let newUrl = '';
if (url?.path) newUrl = url.path + '?';
if (url?.query) {
for (let key in url?.query) {
newUrl += `${
key}=${
url?.query[key]}&`
}
}
return newUrl.slice(0, newUrl?.length - 1)
}
使用:
const b = objToUrl({
path: 'http:baidu.com',
query: {
age: 11,
name: '小花'
}
})
console.log(b)//http:baidu.com?age=11&name=小花
边栏推荐
- Linux install redis database
- MSTP - Multiple Spanning Tree (Case + Configuration)
- 数据分析面试手册《统计篇》
- EN 12467纤维水泥平板产品—CE认证
- Linux安装redis数据库
- BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection 论文笔记
- J9数字论:DAO治理更像一种生态过程:治理原生于网络,不断演变
- Only lazy and hungry. You still don't understand the singleton pattern!
- 分库分表ShardingSphere-JDBC笔记整理
- 微信小程序强制更新版本
猜你喜欢
Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
ABP中的数据过滤器
BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection 论文笔记
Mysql. Slow Sql
YOLOv5的Tricks | 【Trick13】YOLOv5的detect.py脚本的解析与简化
How engineers treat open source
J9 Digital Theory: DAO governance is more like an ecological process: governance is native to the network and continues to evolve
WinForm(五)控件和它的成员
WebView2 通过 PuppeteerSharp 实现RPA获取壁纸 (案例版)
随机推荐
详解JDBC的实现与优化(万字详解)
数据分析面试手册《SQL篇》
异常:try catch finally throws throw
百战RHCE(第四十八战:运维工程师必会技-Ansible学习3-构建Ansible清单)
rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)
Only lazy and hungry. You still don't understand the singleton pattern!
Mysql database installation and configuration detailed tutorial
J9数字论:DAO治理更像一种生态过程:治理原生于网络,不断演变
16. Sum of the nearest three numbers
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
Kunpeng compilation and debugging and basic knowledge of native development tools
22/8/9 Collection of Greedy Problems
版本号大小的判断方法
微信小程序自定义navigationBar
2022.8.10-----leetcode.640
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 ADHK Problem Solving
单片机人机交互--矩阵按键
EPro-PnP: Generalized End-to-End Probabilistic Perspective-n-Points for Monocular Object Pose Est...
深度解析volatile关键字(保证够全面)