JSON 해석 - 더 이상 백 엔 드 키 이름 변경 안 해도 돼 요.
function transTOTree(arr, rule) {
    arr.forEach(item => {
        rule.forEach(map => {
            for (let key in map) {
                let newValue = map.oldKey ? item[map.oldKey] : '';
                if (map.newValue) {
                    newValue = map.newValue(newValue);
                }
                item[map.newKey] = newValue;
            }
        })
        if (item.children && item.children.length > 0) {
            transTOTree(item.children, rule);
        }
    });
    return arr;
}/ / 매개 변수
var params = [{
        oldKey: "name",
        newKey: "title",
        newValue: v => {
            return v;
        }
    },
    {
        oldKey: "is",
        newKey: "checked",
        newValue: v => {
            return v ? 1 : 0;
        }
    }
]
/ / 아 날로 그 데이터
var oldData = [{
    name: 1,
    is: 1,
    children: [{
        name: 11,
        is: 1,
        children: [{
            name: 1111,
            is: 0,
        }]
    }, {
        name: 12,
        is: 0,
    }]
}, {
    name: 2,
    is: 1,
}]
/ / 호출
console.log(transTOTree(oldData, params))
/ / 출력
    [{
        title: 1,
        checked: true,
        children: [{
            title: 11,
            checked: true,
            children: [{
                title: 1111,
                checked: false,
            }]
        }, {
            title: 12,
            checked: false,
        }]
    }, {
        title: 2,
        checked: true,
    }]이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Thymeleaf 의 일반 양식 제출 과 AJAX 제출텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.