json对象及数组键值的深度大小写转换问题详解

前言

最近在做一个项目,发现后端返回的数据键值全部都是大写的,有时候前端用起来很不方便,所以写了一个深度转换的小工具,分享给大家,也就不用重复造轮子了,不喜勿喷,下面话不多说了,来一起看看详细的介绍吧。

start

rush:bash;"> npm i deep-lu-trans --save

json example

t.objKeysToLower(obj).then((res) => {
console.log(JSON.stringify(res));

});
t.objKeysToLower(obj).then((res) => {
console.log(JSON.stringify(res));
/
{"as_dd_s":123213,"and_saj_jdkjsakd":{"djk_asj":{"sa_dsa_dsa":123123,"asda_sdh_kas":{"asd_sad":[{"asd_sad_sa":123123,"iii_asda":[1,{"asd_sad":123}]}]}}},"kio":[{"op":1,"oop":2},{"ol":1,"op":2}]}
/
t.objKeystoupper(res).then((_s) => {
console.log(JSON.stringify(_s));
/
{"AS_DD_S":123213,"AND_SAJ_JDKJSAKD":{"DJK_ASJ":{"SA_DSA_DSA":123123,"ASDA_SDH_KAS":{"ASD_SAD":[{"ASD_SAD_SA":123123,"III_ASDA":[1,{"ASD_SAD":123}]}]}}},"KIO":[{"OP":1,"OOP":2},{"OL":1,"OP":2}]}
/
});
});

array example

const arr = [{
A_B: 1,B_C: 2
},3,4,'abc',[{
A_B: 1,B_C: {
C_D: [0,{
CK: 1,KL: 2
}]
}
}]]

t.arrKeysToLower(arr).then((res) => {
console.log(JSON.stringify(res));
/
[{"a_b":1,"b_c":2},"abc",[{"a_b":1,"b_c":{"c_d":[0,{"ck":1,"kl":2}]}}]]
/
t.arrKeystoupper(res).then((_s) => {
console.log(JSON.stringify(_s));
[{"A_B":1,"B_C":2},[{"A_B":1,"B_C":{"C_D":[0,{"CK":1,"KL":2}]}}]]
});
})

git地址:

nofollow" target="_blank" href="https://github.com/burning0xb/deep-lu-trans">https://github.com/burning0xb/deep-lu-trans

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程之家的支持

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...