当前位置:网站首页>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=小花
边栏推荐
- 构建检测,无规矩不成方圆
- 双机热备综合实验(VRRP+OSPF+VTP+NAT+DHCP+PVSTP+单臂路由)
- C#使用计时器
- 成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
- Mysql database installation and configuration detailed tutorial
- Where can I download IEEE papers?
- Web APIs BOM - A Comprehensive Case of Operating Browsers
- 【ASM】字节码操作 ClassWriter COMPUTE_FRAMES 的作用 与 visitMaxs 的关系
- 微信小程序获取当前页面的url和参数
- 91.(cesium之家)cesium火箭发射模拟
猜你喜欢

How to easily obtain the citation format of references?

分库分表ShardingSphere-JDBC笔记整理

"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 2 DGHJKL Problem Solution

百战RHCE(第四十八战:运维工程师必会技-Ansible学习3-构建Ansible清单)

Shell 文本三剑客 Sed

How to do patent mining, the key is to find patent points, in fact, it is not too difficult

复制带随机指针的链表——LeetCode

ABP中的数据过滤器

Volatile和CAS

YOLOv5的Tricks | 【Trick13】YOLOv5的detect.py脚本的解析与简化
随机推荐
Lens filter---about day and night dual-pass filter
How to determine the size of the version number
How to build speed, speed up again
深度解析volatile关键字(保证够全面)
[Excel knowledge and skills] Convert numeric format numbers to text format
什么是“门”电路(电子硬件)
R language multiple linear regression, ARIMA analysis of the impact of different candidates in the United States on the economic GDP time series
虚拟电厂可视化大屏,深挖痛点精准减碳
【爬虫】scrapy创建运行爬虫、解析页面(嵌套url)、自定义中间件(设置UserAgent和代理IP)、自定义管道(保存到mysql)
HW-蓝队工作流程(1)
url转成obj或者obj转成url的方法
20张图,全面掌握MVCC原理!
Where can I download IEEE papers?
Software Testing Certificate (1) - Software Evaluator
【mysql】mysql分别按年/月/日/周分组统计数据
如何破坏Excel文件,让其显示文件已损坏方法
复制带随机指针的链表——LeetCode
Linux install redis database
ADC和DAC记录
WinForm(五)控件和它的成员