例
let arr = [ { date: '2018-01-06',SW: '90.95', LL: '136', XXSW: '80.22',CKLL: '500' }, { date: '2018-01-06', SW: '164.95', LL: '137', XXSW: '99.22', CKLL: '93' }, { date: '2018-01-07', SW: '176.95', LL: '236', XXSW: '99.22', CKLL: '93' }, { date: '2018-01-07', SW: '178.95', LL: '236', XXSW: '99.22', CKLL: '93' }, { date: '2018-01-07', SW: '182.95', LL: '236', XXSW: '99.22', CKLL: '93' }, { date: '2018-01-08', SW: '187.95', LL: '366', XXSW: '99.22', CKLL: '93' }, { date: '2018-01-09', SW: '197.95', LL: '466', XXSW: '99.22', CKLL: '93' } ]
上面的arr为数组对象,需要根据date进行拆分分类,同一天的要放在一个数组里面,可以用如下代码。
let dataArr = []; arr.map(mapItem => { if (dataArr.length == 0) { dataArr.push({ date: mapItem.date, List: [mapItem] }) } else { let res = dataArr.some(item=> {//判断相同日期,有就添加到当前项 if (item.date == mapItem.date) { item.List.push(mapItem) return true } }) if (!res) {//如果没找相同日期添加一个新对象 dataArr.push({ date: mapItem.date, List: [mapItem] }) } } })
最后dataArr变成
dataArr = [ { date:'2018-01-06', List: [ { date: '2018-01-06', SW: '90.95', LL: '136', XXSW: '80.22', CKLL: '500' }, { date: '2018-01-06', SW: '164.95', LL: '137', XXSW: '99.22', CKLL: '93' } ] }, { date:'2018-01-07', List: [ { date: '2018-01-07', SW: '176.95', LL: '236', XXSW: '99.22', CKLL: '93' }, { date: '2018-01-07', SW: '178.95', LL: '236', XXSW: '99.22', CKLL: '93' }, { date: '2018-01-07', SW: '182.95', LL: '236', XXSW: '99.22', CKLL: '93' }, ] }, { date:'2018-01-08', List: [ { date: '2018-01-08', SW: '187.95', LL: '366', XXSW: '99.22', CKLL: '93' }, ] }, { date:'2018-01-09', List: [ { date: '2018-01-09', SW: '197.95', LL: '466', XXSW: '99.22', CKLL: '93' } ] } ]
点击生成n位随机数
<!DOCTYPE html> <html> <head> <title>点击input生成n位随机数</title> </head> <body> <input type="text" value="Test" id="Test" /> <button id="btn">提交</button> <script> var jschars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; function generateMixed(n) {//n代表生成随机数位数 var res = ""; for(var i = 0; i < n ; i ++) { var id = Math.ceil(Math.random()*35); res += jschars[id]; } Test.value=res; } btn.onclick = function(){ generateMixed(7) } </script> </body> </html>