当前位置:网站首页>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=小花
边栏推荐
- Word set before the title page
- input输入框超出部分用省略号表示以及判断内容是否有超出(PC端)
- ② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
- More parameter exposure of Pico 4: Pancake + color perspective, and Pro version
- 异常:try catch finally throws throw
- 总结Qt中常用文件信息QFileInfo的获取:后缀,名称,路径,链接
- nodejs项目连接mysql数据库
- 成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
- Software protection scenario of NOR FLASH flash memory chip ID application
- 全排列思路详解
猜你喜欢
YOLOv5的Tricks | 【Trick13】YOLOv5的detect.py脚本的解析与简化
Introduction to Qt (6) - Implementation of the lottery system
Elastic scaling of construction resources
【21天学习挑战赛】折半插入排序
数据分析面试手册《统计篇》
apache+PHP+MySQL+word press, page error when installing word press?
MSTP——多生成树(案列+配置)
MySQL进阶查询
Only lazy and hungry. You still don't understand the singleton pattern!
关于编程本质那些事
随机推荐
② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
【考虫 六级英语】语法课笔记
How to build speed, speed up again
MySQL索引与事务
Exceptions and exception handling mechanisms
虚拟电厂可视化大屏,深挖痛点精准减碳
LeetCode_优先级队列_692.前K个高频单词
BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection 论文笔记
【ASM】字节码操作 ClassWriter COMPUTE_FRAMES 的作用 与 visitMaxs 的关系
what is an array
postgresql参数意义
apache+PHP+MySQL+word press, page error when installing word press?
Mysql数据库安装配置详细教程
J9 Digital Theory: DAO governance is more like an ecological process: governance is native to the network and continues to evolve
SystemVerilog: 验证知识点点滴滴
成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
[GXYCTF2019]BabySQli
SQL语句--获取数据库表信息,表名、列名、描述注释等
vim simple save window id
编程技巧│selenium 更新 chromedriver 驱动