프로그래밍/Javascript

JSON 객체 정렬하여 출력하기

테타니 2019. 2. 18. 18:02
console.log(JSON.stringify(객체명, null, 4));


 var myObject = {

  name: ['Bob', 'Smith'],

  age: 32

}; 

라는 JSON 객체가 있다고 가정하면


(1) console.log(myObject); /* 기본으로 출력했을시 */

결과 : {name: Array(2), age: 32}


(2) console.log(JSON.stringify(myObject, null, 4));  /* stringify함수를 이용하여 인자를 넣었을시 */ 

결과 : {

    "name": [

        "Bob",

        "Smith"

    ],

    "age": 32

}