跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。

跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。,第1张

var origin = [

{"first_id":1,"first_name":"中学","second_id":"1-1","second_name":"一年级","third_id":"1-1-1","third_name":"一年级一班","people":10,"age":10,"parent":5},

{"first_id":1,"first_name":"中学","second_id":"1-1","second_name":"一年级","third_id":"1-1-2","third_name":"一年级二班","people":11,"age":10,"parent":5},

{"first_id":1,"first_name":"中学","second_id":"1-2","second_name":"二年级","third_id":"1-2-1","third_name":"二年级一班","people":20,"age":10,"parent":5},

{"first_id":1,"first_name":"中学","second_id":"1-2","second_name":"二年级","third_id":"1-2-2","third_name":"二年级二班","people":21,"age":10,"parent":5},

{"first_id":2,"first_name":"高中","second_id":"2-1","second_name":"一年级","third_id":"2-1-1","third_name":"一年级一班","people":31,"age":10,"parent":5}

];

var finalData = []; // 最终的数据

transferData(); // 数据转换

consolelog(finalData, JSONstringify(finalData));

function transferData() {

originforEach(function(n) {

var first = getRecordById(nfirst_id, finalData);

if (first) {

firstage += nage;

firstparent += nparent;

firstpeople += npeople;

var second = getRecordById(nsecond_id, firstchildren);

if (second) {

secondage += nage;

secondparent += nparent;

secondpeople += npeople;

var third = getRecordById(nthird_id, secondchildren);

if (third) {

// 这里应该不会存在

} else {

secondchildrenpush({

id: nthird_id,

name: nthird_name,

age: nage,

parent: nparent,

people: npeople

});

}

} else {

firstchildrenpush({

id: nsecond_id,

name: nsecond_name,

age: nage,

parent: nparent,

people: npeople,

children: [{

id: nthird_id,

name: nthird_name,

age: nage,

parent: nparent,

people: npeople

}]

});

}

} else {

finalDatapush({

id: nfirst_id,

name: nfirst_name,

age: nage,

parent: nparent,

people: npeople,

children: [{

id: nsecond_id,

name: nsecond_name,

age: nage,

parent: nparent,

people: npeople,

children: [{

id: nthird_id,

name: nthird_name,

age: nage,

parent: nparent,

people: npeople

}]

}]

});

}

});

}

function getRecordById(id, data) {

for (var i = 0, n = datalength; i < n; i++) {

if (data[i]id == id) return data[i];

}

return null;

}

原生js遍历json对象

遍历json对象:

无规律:

有规律:

遍历json对象

有如下 json对象:

var obj ={"name":"冯娟","password":"123456","department":"技术部","sex":"女","old":30};

遍历方法:

数组对象的遍历:

下面我们对照一下js和jquer的遍历方法:

原文网址: >

看这样一段函数定义的代码就明白了function json_to_format_string(obj,deep){ var html = ""; var blank_string = ""; for (var i = 0; i < deep; i++) { blank_string += " "; }; for(var name in obj){ if (typeof(obj[name])=="object") { html += blank_string + name +": "+ "

"; html += json_to_format_string(obj[name],deep+1); }else{ html += blank_string + name +": "+ obj[name]+"

"; } } return html;}

以上就是关于跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。全部的内容,包括:跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。、js遍历json对象(推荐)、js怎样遍历json数组或对象并读取相应的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10184727.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存