当前位置:网站首页>url转成obj或者obj转成url的方法
url转成obj或者obj转成url的方法
2022-08-11 00:36:00 【琴~~】
url转成obj或者obj转成url的方法
1、url转成obj
function urlToObj(url) {
if (typeof url !== 'string') return '请传入正确的参数类型'
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=小花
边栏推荐
- How to easily obtain the citation format of references?
- 分库分表ShardingSphere-JDBC笔记整理
- 【爬虫】scrapy创建运行爬虫、解析页面(嵌套url)、自定义中间件(设置UserAgent和代理IP)、自定义管道(保存到mysql)
- rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)
- [Data Visualization] Chart Design Principles
- “蔚来杯“2022牛客暑期多校训练营3 DF题解
- 复制带随机指针的链表——LeetCode
- 构建检测,无规矩不成方圆
- Difference Between Image Recognition and Semantic Segmentation
- 【openpyxl】过滤和排序
猜你喜欢
Shell编程三剑客之sed
C#-委托的详细用法
如何便捷获取参考文献的引用格式?
input输入框超出部分用省略号表示以及判断内容是否有超出(PC端)
Distributed. Performance optimization
In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?
虚拟电厂可视化大屏,深挖痛点精准减碳
③ 关系数据库标准语言SQL 数据查询(SELECT)
Kunpeng compilation and debugging and basic knowledge of native development tools
工程师如何对待开源
随机推荐
【经典排序】快速排序
Kunpeng compilation and debugging and basic knowledge of native development tools
详谈二叉搜索树
异常:try catch finally throws throw
② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
Software protection scenario of NOR FLASH flash memory chip ID application
Lens filter---about day and night dual-pass filter
rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)
两个链表的第一个公共节点——LeetCode
循环单词
More parameter exposure of Pico 4: Pancake + color perspective, and Pro version
what is an array
【pypdf2】安装、读取和保存、访问页面、获取文本、读写元数据、加密解密
微信小程序自定义navigationBar
LeetCode_优先级队列_692.前K个高频单词
小程序onPageNotFound的坑
【openpyxl】过滤和排序
BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection 论文笔记
electron -autoUpdater 更新
2022.8.10-----leetcode.640