Home > Web Front-end > JS Tutorial > How to operate json objects and array key value case conversion

How to operate json objects and array key value case conversion

php中世界最好的语言
Release: 2018-05-26 10:20:44
Original
1756 people have browsed it

This time I will show you how to operate jsonobjectandarraykey value case conversion, and how to operate json object and array key value case conversionNotesWhat are they? The following is a practical case. Let’s take a look.

Preface

I was working on a project recently and found that all the data key values ​​returned by the backend are in uppercase letters, which is sometimes used by the frontend. It's very inconvenient, so I wrote a small deep conversion tool to share with everyone, so that I don't have to reinvent the wheel. If you don't like it, don't criticize it. I won't say much below, let's take a look at the detailed introduction.

start

npm i deep-lu-trans --save
Copy after login

json example

import t from 'deep-lu-trans';
const obj = {
 AS_DD_S: 123213,
 AND_SAJ_JDK_JSAKD_: {
 DJK_ASJ: {
  SA_DSA_DSA: 123123,
  ASDA_SDH_KAS: {
  ASD_SAD: [{
   ASD_SAD_SA: 123123,
   III_ASDA: [1, 23, {
   ASD_SAD: 123
   }]
  }]
  }
 }
 },
 KIO: [{
 OP: 1,
 OOP: 2
 }, {
 OL: 1,
 OP: 2
 }]
};
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_jdk_jsakd_":{"djk_asj":{"sa_dsa_dsa":123123,"asda_sdh_kas":{"asd_sad":[{"asd_sad_sa":123123,"iii_asda":[1,23,{"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_JDK_JSAKD_":{"DJK_ASJ":{"SA_DSA_DSA":123123,"ASDA_SDH_KAS":{"ASD_SAD":[{"ASD_SAD_SA":123123,"III_ASDA":[1,23,{"ASD_SAD":123}]}]}}},"KIO":[{"OP":1,"OOP":2},{"OL":1,"OP":2}]}
  */
 });
});
Copy after login

array example

import t from 'deep-lu-trans';
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},3,4,"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},3,4,"abc",[{"A_B":1,"B_C":{"C_D":[0,{"CK":1,"KL":2}]}}]]
 });
})
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to make js convert the key value string into a json string

How to use angular4 in Data communication among multiple components

The above is the detailed content of How to operate json objects and array key value case conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template