json 키 정렬 함수 만든 것

ino5·2023년 10월 24일
0

23년 회사

목록 보기
6/6

json 키 정렬 함수


const order = function(paramObj) {
  const result = {};
  Object.keys(paramObj).sort().map(key => {
    const value = paramObj[key];
    if (Array.isArray(value)) { // 배열일 경우
      for (let i = 0; i < value.length; i++) {
        const el = value[i];
        if (typeof el != 'object') continue; // 배열/객체가 아닐 경우
        value[i] = order(el);
      }
      result[key] = value;
      
    } else if (typeof value == 'object') { // 객체일 경우
      result[key] = order(value);
    
    } else {
      result[key] = value;  
    }
  });
  return result;
}

profile
지금은 네이버 블로그만 해요... https://blog.naver.com/chero77

0개의 댓글