js,json数组遍历 有点难 跪求大神
vara=[{"id":"0000","name":"ROOT","children":[{"id":"1000","name":"1年","children":[{"i...
var a = [ {
"id" : "0000",
"name" : "ROOT",
"children" : [ {
"id" : "1000",
"name" : "1年",
"children" : [ {
"id" : "1001",
"name" : "1年1"
} ]
}, {
"id" : "2000",
"name" : "2年"
} ]
} ];
var b = [ {
"class_id" : "0000",
"age" : 1,
"pjz" : 2
}, {
"class_id" : "1000",
"age" : 3,
"pjz" : 3
}, {
"class_id" : "1001",
"age" : 3,
"pjz" : 5
}, {
"class_id" : "2000",
"age" : 2,
"pjz" : 8
} ];
var c = [ {"field" : "age","type" : "sum"},{"field" : "pjz","type" : "default"},{"field" : "id","type" : "default"}];
var d = [ {
"id" : "0000",
"name" : "ROOT",
"age" : 9,--->为下面所属节点为age的和
"pjz" : 2,---->不变
"children" : [ {
"id" : "1000",
"name" : "1年",
"age" : 6,--->为下面所属节点为age的和
"pjz" : 3,
"children" : [ {
"id" : "1001",
"name" : "1年1",
"age" : 3,
"pjz" : 5
} ]
}, {
"id" : "2000",
"name" : "2年",
"age" : 2,
"pjz" : 8
} ]
} ];
以上json数组根据a,b,c数组得到d
先根据a中id和b中class_id对应关系将b里面除class_id的对应放到a中
遍历根据1得出的json数组到c中field对应关系,若type为sum的进行求和,为default不变
跪求大神
先根据a中id和b中class_id对应关系将b里面除class_id的对应放到a中//这步自己已经完成 展开
"id" : "0000",
"name" : "ROOT",
"children" : [ {
"id" : "1000",
"name" : "1年",
"children" : [ {
"id" : "1001",
"name" : "1年1"
} ]
}, {
"id" : "2000",
"name" : "2年"
} ]
} ];
var b = [ {
"class_id" : "0000",
"age" : 1,
"pjz" : 2
}, {
"class_id" : "1000",
"age" : 3,
"pjz" : 3
}, {
"class_id" : "1001",
"age" : 3,
"pjz" : 5
}, {
"class_id" : "2000",
"age" : 2,
"pjz" : 8
} ];
var c = [ {"field" : "age","type" : "sum"},{"field" : "pjz","type" : "default"},{"field" : "id","type" : "default"}];
var d = [ {
"id" : "0000",
"name" : "ROOT",
"age" : 9,--->为下面所属节点为age的和
"pjz" : 2,---->不变
"children" : [ {
"id" : "1000",
"name" : "1年",
"age" : 6,--->为下面所属节点为age的和
"pjz" : 3,
"children" : [ {
"id" : "1001",
"name" : "1年1",
"age" : 3,
"pjz" : 5
} ]
}, {
"id" : "2000",
"name" : "2年",
"age" : 2,
"pjz" : 8
} ]
} ];
以上json数组根据a,b,c数组得到d
先根据a中id和b中class_id对应关系将b里面除class_id的对应放到a中
遍历根据1得出的json数组到c中field对应关系,若type为sum的进行求和,为default不变
跪求大神
先根据a中id和b中class_id对应关系将b里面除class_id的对应放到a中//这步自己已经完成 展开
展开全部
代码太长,看演示吧。
http://jsfiddle.net/2vxsbtkx/
http://jsfiddle.net/2vxsbtkx/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询