已知以下数组:arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]];brr=['date','type','val'];如何构建以下数组:crr=[{'date':'2018-01-01','type':'aaa','val':12.11},{...},{...},{...}];菜鸟,对数组理解不深,可否分别使用JavaScript、jQuery两种方式演示;
1 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
var arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]]; var brr=['date','type','val']; function js(){ var crr = new Array(); for (var i = 0; i < arr.length; i++){ var a = new Array(); a[brr[0]]= arr[i][0]; a[brr[1]]= arr[i][1]; a[brr[2]]= arr[i][2]; crr.push(a); } } function jq(){ var crr = new Array(); $.each(arr, function(i, e){ var a = new Array(); a[brr[0]]= e[0]; a[brr[1]]= e[1]; a[brr[2]]= e[2]; crr.push(a); }); } js(); jq();
添加回答
举报
0/150
提交
取消