js获取多层json最深层级数
var data = { "list": [{ "list": [ { "list": [{ "list": [{"list": [{"four": "4", "one": "1", "three": "3", "two": "3"}], "value": "4"}], "value": "3" }, { "list": [{ "list": [{"four": "4", "one": "1", "three": "4", "two": "3"}, { "four": "4", "one": "1", "three": "4", "two": "3" }], "value": "4" }], "value": "4" }], "value": "3" }], "value": "1" }, { "list": [{ "list": [{ "list": [{"list": [{"four": "4", "one": "2", "three": "3", "two": "2"}], "value": "4"}], "value": "3" }], "value": "2" }], "value": "2" }] }
var num = 0; function recursion(obj,k) { num = Math.max(num,k); console.log(k); if (obj.list) obj.list.forEach(function(v, i){ recursion(v,k+1); }); } recursion(data,1); console.log("最深层级数"+num);
我遇到的问题,得到大神解决,特此分享