1,时间转换
formatDate ( now) {
var year = new Date ( now) . getFullYear ( ) ;
var month = new Date ( now) . getMonth ( ) + 1 ;
var date = new Date ( now) . getDate ( ) ;
var hour = new Date ( now) . getHours ( ) ;
var minute = new Date ( now) . getMinutes ( ) ;
var second = new Date ( now) . getSeconds ( ) ;
if ( month < 10 ) {
month = '0' + month;
}
if ( date < 10 ) {
date = "0" + date;
}
if ( hour < 10 ) {
hour = "0" + hour;
}
if ( minute < 10 ) {
minute = "0" + minute;
}
if ( second < 10 ) {
second = "0" + second;
}
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
} ,
var aa = formatDate ( 1624265374 * 1000 )
getWeek ( dateString) {
var dateArray = dateString. split ( "-" ) ;
var date = new Date ( dateArray[ 0 ] , parseInt ( dateArray[ 1 ] - 1 ) , dateArray[ 2 ] ) ;
return "星期" + "日一二三四五六" . charAt ( date. getDay ( ) ) ;
} ,
console. log ( getWeek ( '2021-06-21' ) )
function countDown ( time) {
var date1 = new Date ( ) . getTime ( ) ;
var date2 = time * 1000 ;
var date3 = date2 - date1;
var days = Math. ceil ( date3 / ( 24 * 3600 * 1000 ) ) ;
return days;
}
console. log ( countDown ( 1624438173 ) )
2, 字符串和数值处理
changeNumType ( value) {
if ( value >= 10000 && value < 10000000 ) {
value = ( value / 10000 ) . toFixed ( 2 ) + "万" ;
} else if ( value >= 10000000 && value < 100000000 ) {
value = ( value / 10000000 ) . toFixed ( 2 ) + "千万" ;
} else if ( value >= 100000000 ) {
value = ( value / 100000000 ) . toFixed ( 2 ) + "亿" ;
} else if ( value < 10000 ) {
value = value;
}
return value;
} ,
js常用内置对象有:Date, 、Array、String、Math...
Array对象中常用方法:
concat ( ) 表示把几个数组合并成一个数组
join ( ) 设置分隔符连接数组元素为一个字符串
pop ( ) 移除数组最后一个元素
shift ( ) 移除数组中第一个元素
slice ( start,end) 返回数组中的一段
splice ( ) 可以用来删除,可以用来插入,也可以用来替换
push ( ) 往数组中新添加一个元素,返回最新长度
sort ( ) 对数组进行排序
reverse ( ) 反转数组的顺序
toLocaleString ( ) 把数组转换为本地字符串
array对象属性:
length 表示取得当前数组长度 (常用)
constructor 引用数组对象的构造函数
prototype 通过增加属性和方法扩展数组定义
String对象
charAt ( ) 返回指定索引的位置的字符
indexOf ( ) 从前向后检索字符串,看是否含有指定字符串
lastIndexOf ( ) 从后向前检索字符串,看是否含有指定字符串
concat ( ) 连接两个或多个字符串
match ( ) 使用正则表达式模式对字符串执行查找,并将包含查找结果最为结果返回
replace ( ) 替换一个与正则表达式匹配的子串
search ( ) 检索字符串中与正则表达式匹配的子串。如果没有找到匹配,则返回 - 1 。
slice ( start,end) 根据下表截取子串
substring ( start,end) 根据下表截取子串
split ( ) 根据指定分隔符将字符串分割成多个子串,并返回素组
substr ( start,length) 根据长度截取字符串
toUpperCase ( ) 返回一个字符串,该字符串中的所有字母都被转化为大写字母。
toLowerCase ( ) 返回一个字符串,该字符串中的所有字母都被转化为小写字母。
Math对象
ceil ( ) 向上取整。
floor ( ) 向下取整。
round ( ) 四舍五入。
random ( ) 取随机数。
3, 处理数组对象函数
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " lodash.js" > </ script>
</ head>
< body>
< div> 测试数据转换</ div>
< script>
const empsRows = [
{ RowId: '001' , EmpId: '10' , Lastname: 'Smith' , Firstname: 'Joe' , Salary: 40000 } ,
{ RowId: '002' , EmpId: '12' , Lastname: 'Jones' , Firstname: 'Mary' , Salary: 50000 } ,
{ RowId: '003' , EmpId: '11' , Lastname: 'Johnson' , Firstname: 'Cathy' , Salary: 44000 } ,
{ RowId: '004' , EmpId: '22' , Lastname: 'Jones' , Firstname: 'Bob' , Salary: 55000 } ,
{ RowId: '005' , EmpId: '24' , Lastname: 'Steve' , Firstname: 'Mike' , Salary: 62000 }
]
function applyColumn ( colDataset, columnName) {
if ( ! _. has ( colDataset, columnName) ) {
colDataset[ columnName] = [ ]
}
return colDataset
}
function rowOriented2ColOriented ( rowDataset) {
let colDataset = { }
rowDataset. forEach ( function ( row, i) {
const columnNames = _. keys ( row)
columnNames. forEach ( function ( columnName) {
colDataset = applyColumn ( colDataset, columnName)
colDataset[ columnName] [ i] = row[ columnName]
} )
} )
return colDataset
}
const transformedDataset = rowOriented2ColOriented ( empsRows)
console. log ( transformedDataset)
</ script>
</ body>
</ html>