Lang/Javascript
JS * JSON ๋ฐ์ดํฐ KEY๋ง UpperCase ํ๊ธฐ ! ( ์์ฉํ๋ฉด LowerCase ๋ ๊ฐ๋ฅ! )
___l_i_ *
2021. 12. 20. 16:52
JSON ๋ฐ์ดํฐ์ KEY ๋ค์ด๋ฐ์ด ์ ๋ง.. ๋๋ฌด ์์๋๋ผ.. ใ
์ฐพ์๋ณด๋ ์ค ์ข์ ์์ค๋ฅผ ๋ฐ๊ฒฌํ๊ณ ์ผ๋ถ ์์ ํด์ ์ฌ์ฉํ๊ธฐ๋ก ํ๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ค์นํ ๋งํ ๋ด์ฉ๋ ์๋๊ฑฐ ๊ฐ๊ตฌ..
์ฝ๋์ ๋ฑ! ํ๊ณ ๋ชจ๋ ๋ด์ฉ์ด ๋ณด์ด๋ ์์ธ๋ด์ฉ์ ์๋ตํฉ๋๋ค :)
key๋ฅผ ๋ชจ๋ ์๋ฌธ์๋ก ๋ฐ๊พธ๊ณ ์ถ์ผ์๋ฉด toUpperCase() ๋ถ๋ถ์ toLowerCase()๋ก ๋ฐ๊พธ๋ฉด ๋๊ฒ ์ ธ!!
function jsonKeyUpperCase(object){
if(Array.isArray(object)){
// ๋ฆฌ์คํธ<๋งต> ํ์์ผ๋ก ๋์ด์ค๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
object.forEach((item, index) =>{
object[index] = Object.fromEntries(Object.entries(item).map(([key, value]) => [key.toUpperCase(), value]));
});
return object;
}
else {
// ๋งต ํ์์ผ๋ก ๋์ด์ค๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
return Object.fromEntries(Object.entries(object).map(([key, value]) => [key.toUpperCase(), value]));
}
}
์ฌ์ฉํด๋ณด์ !
let a = {test: 'tetseTTTT'};
let b = [
{tt: 'ttttT', Aa: 'aaAaa', BB: 'aaabbbbb'},
{tt2: 'ttttT', Aa2: 'aaAaa', BB2: 'aaabbbbb'}
]
a = jsonKeyUpperCase(a);
b = jsonKeyUpperCase(b);
Thanks for ๐