json 객체를 다른 json 객체로 재귀 적으로 변환하는 데 도움이 필요합니다.json 객체를 재귀 적으로 변환합니다.
내 입력 오브젝트는 다음과 같습니다
이var my_obj = {
"id": 22,
"eventTypeId": 37,
"eventTypeName": "CommonOnly",
"parentEvent": {
"id": 21,
"eventTypeId": 35,
"eventTypeName": "FullTest",
"parentEvent": {
"id": 20,
"parentEvent": null,
"eventTypeId": 38,
"eventTypeName": "FullTest"
}
},
"childrenEvents": [
{
"id": 24,
"parentEventId": 22,
"eventTypeId": 36,
"eventTypeName": "BlindedTest",
"childrenEvents": []
},
{
"id": 23,
"parentEventId": 22,
"eventTypeId": 38,
"eventTypeName": "OneCustom",
"childrenEvents": []
}
]
}
내가 원하는 출력은 다음과 같습니다
var my_obj = {
"id": 20,
"eventTypeId": 38,
"parentEvent": null,
"eventTypeName": "FullTest",
"childrenEvents": [{
"id": 21,
"eventTypeId": 35,
"eventTypeName": "FullTest",
"childrenEvents": [
{
"id": 22,
"eventTypeId": 37,
"eventTypeName": "CommonOnly",
"childrenEvents": [
{
"id": 24,
"parentEventId": 22,
"eventTypeId": 36,
"eventTypeName": "BlindedTest",
"childrenEvents": []
}, {
"id": 23,
"parentEventId": 22,
"eventTypeId": 38,
"eventTypeName": "OneCustom",
"childrenEvents": []
}
]
}
]
}]
}
그래서 내가 객체를 얻을 수있을하려는 두 번째 parentEvent
parentEvent = null
는 배열 childrenEvents
과 루트 요소가되었고, 하위 배열은 그 자체가있는 부모 이벤트 객체가됩니다 childrenEvents
배열 및이 배열의 내부에 기존 루트 객체를 넣고 자합니다. childrenEvents
배열
누군가가 문제를 해결하는 데 도움이된다면 고맙게 생각합니다. 미리 감사드립니다.
내 시도는
function rebuild(input)
{
var childrenEvents = [] ;
for (f in input.parentEvent) // we may have attributes other than "age"
if (f.parentEvent == null) {
f.parentEvent.push(childrenEvents);
}
else {
f.parentEvent.push(input.parentEvent[f]);
}
if (input.childrenEvents.length !== 0)
{
input.parentEvent[f].push(input.childrenEvents)
}
return input;
}
console.log(rebuild($scope.myObj))
});
누군가가 SO 당신을 위해 이것을 코드 및 기대처럼 그렇지 않으면 그냥 보이는 .... 당신이 작동하지 않는 시도 어떤 코드 작성 서비스 – charlietfl
되지 제시해주십시오 @charlietfl 몇 분합시다. – antonyboom
@charlietfl 업데이트 대답을 확인하십시오 – antonyboom