在js前端取得json数据后想些处理达到期望的效果。数据:data1=[{"id":1,"name":"a"},{"id":2,"name":"b"}];data2=[{"id":1,"sex":"man"},{"id":2,"sex":"man"},{"id":3,"sex":"woman"},{"id":4,"sex":"woman"}];期望:data3=[{"id":1,"name":"a","info":[{"id":1,"sex":"man"},{"id":2,"sex":"man"},{"id":3,"sex":"woman"},{"id":4,"sex":"woman"}],},{"id":2,"name":"b","info":[{"id":1,"sex":"man"},{"id":2,"sex":"man"},{"id":3,"sex":"woman"},{"id":4,"sex":"woman"}],}];我该怎么写,我的想法是把data1的每个数组取出来,然后往里面用push插入data2的数据,然后将data1的数组重新合并得出结果。写法如下:for(vari=0;ivarinfo=[]; varnewArr=[];info[i].push(data2);newArr.push(info[i]);}returnnewArr;不知道这样写可以嘛。
拉丁的传说
慕少森
相关分类