原数据:vardata=[{"name":"小明","age":"4","birthday":"5-1"},{"name":"小黄","age":"4","birthday":"5-1"},{"name":"小红","age":"4","birthday":"5-3"},]希望整理后得到:varnewArr=[{"birthday":"5-1","detailList":"[{'name':'小明','age':'4'},{'name':'小黄','age':'4'}]"},{"birthday":"5-3","detailList":"[{'name':'小红','age':'4'}]"}];我的思路;data数组第0项直接push(birthday+detailList)整个到新数组newArr,但从第1项就要开始去newArr比较birthday,如果birthday相同,就只用push(detailList),如果不同就push{birthday+detailList}遇到问题:重复打印了一些数据我的代码:`vardata=[{"name":"小明","age":"4","birthday":"5-1"},{"name":"小黄","age":"4","birthday":"5-1"},{"name":"小红","age":"4","birthday":"5-3"},]//[希望得到]//varnewArr=[//{//"birthday":"5-1",//"detailList":"[{'name':'小明','age':'4'},{'name':'小黄','age':'4'}]"//},//{//"birthday":"5-3",//"detailList":"[{'name':'小红','age':'4'}]"//}//];vararr=[];//存放新数组for(vari=0;i
2 回答
至尊宝的传说
TA贡献1789条经验 获得超10个赞
下面这段代码修改如下://第1个就要开始找新数组arr相同的日期else{varindex=-1;//通过for循环查找indexfor(vark=0;kif(resBirthday==arr[k].birthday){ index=k;break;}}if(index===-1){//没有找到arr.push(valItem);}else{//找到了arr[k].detailList.push(valDetailList);}}
添加回答
举报
0/150
提交
取消