方法汇总
据时间范围生成时间刻度数据
function createTimeUnitListByTimeRange ( startDate, endDate, timeUnit) { let startSeconds = new Date ( startDate) . getTime ( ) ; let endSeconds = new Date ( endDate) . getTime ( ) ; let base = new Date ( '1970-01-01 00:00:00' ) . getTime ( ) ; let rangeTimeUnitList = [ ] ; let firstDegree; firstDegree = startSeconds - ( startSeconds - base) % timeUnit; rangeTimeUnitList. push ( firstDegree) ; while ( firstDegree< endSeconds) { firstDegree += timeUnit; rangeTimeUnitList. push ( firstDegree) ; } return rangeTimeUnitList; } console . log ( createTimeUnitListByTimeRange ( '2020-01-01' , '2020-09-10' , 86400000 ) , '测试' ) ;
数组遍历-分类
let arr = [ { spid: '01' , gysid: "a" , spname: "商品1" , price: 2 } , { spid: '02' , gysid: "a" , spname: "商品2" , price: 21 } , { spid: '03' , gysid: "b" , spname: "商品3" , price: 56 } , { spid: '04' , gysid: "c" , spname: "商品4" , price: 21 } , { spid: '05' , gysid: "c" , spname: "商品5" , price: 23 } ] let obj = arr. reduce ( ( last, item) => { if ( last[ item. gysid] ) { last[ item. gysid] . push ( item) } else { last[ item. gysid] = [ item] } return last
} , { } )
console . log ( obj)
{ a: [ { spid: '01' , gysid: 'a' , spname: '商品1' , price: 2 } , { spid: '02' , gysid: 'a' , spname: '商品2' , price: 21 } ] , b: [ { spid: '03' , gysid: 'b' , spname: '商品3' , price: 56 } ] , c: [ { spid: '04' , gysid: 'c' , spname: '商品4' , price: 21 } , { spid: '05' , gysid: 'c' , spname: '商品5' , price: 23 } ]
}
数组去重-对象1
const a= [ { name: 'cy' , age: 10 ,
} , { name: 'cy' , age: 18 , } , { name: 'zq' , age: 16 , } ,
]
let obj1= { }
a. forEach ( item=> { obj1[ item. name] = item
} )
console . log ( Object. values ( obj1) )
数组去重-对象2
const uniqueFun = ( arr, sx) => { let obj = { } ; let peon = arr. reduce ( ( cur, next) => { obj[ next[ sx] ] ? '' : ( obj[ next[ sx] ] = true && cur. push ( next) ) ; return cur; } , [ ] ) ; return peon;
} ;
console . log ( a, uniqueFun ( a, 'name' ) )