js遍历json数组,求大神指导
varjson1=[{"age":5,"num":2,"people":3,"class":9},{"age":1,"num":3,"people":2,"class":...
var json1=[{"age":5,"num":2,"people":3,"class":9},{"age":1,"num":3,"people":2,"class":8},{"age":4,"num":6,"people":5,"class":3}];
var json2=[{"field":"age","typle":"sum","title":"ss"},{"field":"num","typle":"sum","title":"ss"},{"field":"people","typle":"sum","title":"ss"},{"class":"age","typle":"nosum","title":"ss"}]
如上json1、json2两个数组,拿json1里age、num等属性去json2里遍历,
若json2里field的值和json1的属性相等并且其typle的值为num,就计算json1里该属性的和
最终再拼接成下面的样子
var json3=[{"name":"求和","age":10,"num":11,"people":10}];
注:json1里属性未知不光有age、num、people、class只是举个例子
求大神。。。
抱歉 json2写错了 应该是
var json2=[{"field":"age","typle":"sum","title":"ss"},{"field":"num","typle":"sum","title":"ss"},
{"field":"people","typle":"sum","title":"ss"},{"field":"class","typle":"nosum","title":"ss"}]
//没人会? 求源码 展开
var json2=[{"field":"age","typle":"sum","title":"ss"},{"field":"num","typle":"sum","title":"ss"},{"field":"people","typle":"sum","title":"ss"},{"class":"age","typle":"nosum","title":"ss"}]
如上json1、json2两个数组,拿json1里age、num等属性去json2里遍历,
若json2里field的值和json1的属性相等并且其typle的值为num,就计算json1里该属性的和
最终再拼接成下面的样子
var json3=[{"name":"求和","age":10,"num":11,"people":10}];
注:json1里属性未知不光有age、num、people、class只是举个例子
求大神。。。
抱歉 json2写错了 应该是
var json2=[{"field":"age","typle":"sum","title":"ss"},{"field":"num","typle":"sum","title":"ss"},
{"field":"people","typle":"sum","title":"ss"},{"field":"class","typle":"nosum","title":"ss"}]
//没人会? 求源码 展开
展开全部
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
</body>
<script>
var json1=[
{"age":5,"num":2,"people":3,"class":9},
{"age":1,"num":3,"people":2,"class":8},
{"age":4,"num":6,"people":5,"class":3}
],
json2=[
{"field":"age","typle":"sum","title":"ss"},
{"field":"num","typle":"sum","title":"ss"},
{"field":"people","typle":"sum","title":"ss"},
{"field":"class","typle":"nosum","title":"ss"}
],
typeArr = {'sum': 'sum', 'nosum': ''};
function getData(json1, json2, typeArr) {
var j = 0,
len1 = json1.length,
len2 = json2.length,
o = {},
json3 = [];
for(;j < len2; j++) {
var j2 = json2[j];
if(j2.field) {
var type = typeArr[j2.typle],
num = -1,
i = 0;
for(var ii = 0; ii < json3.length; ii++) {
var j3 = json3[ii];
if(j3.type === type) {
o = clone(j3);
num = ii;
}
}
for(; i < len1; i++) {
var j1 = json1[i];
if(type === 'sum') {
o[j2.field] = o[j2.field] ? o[j2.field] + j1[j2.field] : j1[j2.field];
}
}
if(type) {
o.type = type;
if(num !== -1) {
json3[num] = o;
o = {};
}else {
json3[json3.length] = o;
}
}
}
}
return json3;
}
//深copy
function clone(o){
var k,
ret = o,
b;
if(o && ((b = (o instanceof Array)) || o instanceof Object)) {
ret = b ? [] : {};
for(k in o){
if(o.hasOwnProperty(k)){
ret[k] = clone(o[k]);
}
}
}
return ret;
}
//get result
console.log(getData(json1, json2, typeArr));
</script>
</html>
展开全部
嵌套遍历,
for JSON.parse(json1) {
for JSON.parse(json2){
if()....
}
}
自己转换为javascript或者jquery
for JSON.parse(json1) {
for JSON.parse(json2){
if()....
}
}
自己转换为javascript或者jquery
追问
帮忙写了?新手
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以用ext或者是jQuery框架.
追问
可否帮忙写下
追答
亲测可用,有点繁琐但不是很难.你参考一下.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询