SOURCE

var arr =[
    {
        "num":0,
        "child":[
            {
                "num":0,
                "child":[
                    {
                        "num":0,
                        "child":[
                            {
                                "age":1
                            },
                            {
                                "age":2
                            }
                        ]
                    }
                ]
            },
            {
                "num":0,
                "child":[
                    {
                        "age":1
                    },
                    {
                        "age":2
                    }
                ]
            },
            {
                "num":0,
                "child":[
                    {
                        "num":0,
                        "child":[
                            {
                                "age":1
                            },
                            {
                                "age":2
                            }
                        ]
                    },
                    {
                        "num":0,
                        "child":[
                            {
                                "age":1
                            },
                            {
                                "age":2
                            }
                        ]
                    }
                ]
            }
        ]
    }
];




function handlerNum(list){
	for(var i=0;i<list.length;i++){
		if(list[i].child && list[i].child.length>0){
			handlerNum(list[i].child);
			var subSum = 0;
			for(var j=0;j<list[i].child.length;j++){
				subSum = subSum+list[i].child[j].num;;
			}
			list[i].num = subSum;
		}else if(!list[i].num){
			list[i].num = 1;
		}
	}
	
}

handlerNum(arr)


console.log(arr);
console 命令行工具 X clear

                    
>
console