var testObj = {
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
};
처럼 생긴 계층형 객체가 있다고 했을 때
function jsonSpliter(object, result){
Object.keys(object).map(function (key) {
if(typeof object[key] === 'object'){
jsonSpliter(object[key], result);
}else{
result[key] = object[key];
}
});
return result;
}
var funcTest = jsonSpliter(testObj, {});
console.log(funcTest);
이 함수를 사용하면 1차원으로 정리된 결과값을 반환받을 수 있습니다. 필요해서 만들었는데 유용하게 쓰일 수 있을 것 같아 처음으로 글을 남겨봅니다!