由于遇到相关序列化的问题,但是vue项目中由于减少队jquery引用的限制,导致不能用$.param来序列化参数,所以写了如下方法用来解决相关问题,但由于考虑不全,可能存在判断不全或者代码冗余等情况,希望多提意见,多多改善
var personObj = {
name:'cheny0815',
age:24,
c:[{
id:1,
name:2
},{
id:2,
name:3
}],
other:{
a:1,
b:{
c:2,
d:{
a:1,
b:{
e:1,
f:2
}
}
}
},
}
var nextStr = '';
function changeDataType(obj){
let str = ''
if(typeof obj == ' '){
for(let i in obj){
if(typeof obj[i] != 'function' && typeof obj[i] != ' '){
str += i + '=' + obj[i] + '&' ;
}else if (typeof obj[i] == ' '){
nextStr = '';
str += changeSonType(i, obj[i])
}
}
}
return str.replace(/&$/g, '');
}
function changeSonType(objName, objValue){
if(typeof objValue == ' '){
for(let i in objValue){
if(typeof objValue[i] != ' '){
let value = objName + '[' + i + ']=' + objValue[i];
nextStr += encodeURI(value) + '&';
}else{
changeSonType(objName + '[' + i + ']', objValue[i]);
}
}
}
return nextStr;
}
var resultParam = $.param(personObj);
var resultMyself = changeDataType(personObj);
document.write('resultMyself===>' + resultMyself + '<br><hr>')
document.write('resultParam ===>' + resultParam + '<br><hr>')
document.write('resultMyself === resultParam ===>' + (resultMyself === resultParam))
结果如下:


总结
以上所述是小编给大家介绍的原生JS实现$.param() 函数,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
继续阅读与本文标签相同的文章
-
mvc 注解相应配置
2026-05-17栏目: 教程
-
Cassandra Time Window Compaction Strategy (TWCS) :工作原理以及使用建议
2026-05-17栏目: 教程
-
ApacheCon 2019 Cassandra分会各大议题深度剖析,解读cassandra前沿工作
2026-05-17栏目: 教程
-
Aliyun Serverless VSCode Extension v1.12.0 发布
2026-05-17栏目: 教程
-
从5个方面让你真正了解Java内存模型
2026-05-17栏目: 教程
