遍历某个数组里的字段,拼接到需要的对象里,相对应的修改字段名称(代码实例)
程序员文章站
2022-05-07 22:00:09
遍历某个数组里的字段,拼接到需要的对象里,相对应的修改字段名称(代码实例)
var aa = {
"people": [
{...
遍历某个数组里的字段,拼接到需要的对象里,相对应的修改字段名称(代码实例)
var aa = { "people": [ { "id": 1, "name": "peng", "lastName": "yaping", "email": "6666666@qq.com" } ], "values":[] } var bb = [ {"years":"2016","day":"aaa"}, {"years":"2017","day":"bbb"}, {"years":"2018","day":"ccc"}, ] //遍历数组 for循环 for(var i=0;i<bb.length;i++){ //console.log("for遍历数组",bb[i].years); } //遍历数组for in for(var i in bb){ //console.log("for in遍历数组",bb[i]); //数据结构根据后端的API文档 //这里的操作,有时需要的对象里面的字段名称要改变 var cc ={}; cc.enen = bb[i].years; cc.chch = bb[i].day; console.log(cc); aa.values.push(cc); } console.log(aa);