1.字符串(string)
-
字符串转化大小写,字符串首字母转化为大写
let a = 'ab cd ef' let b = a.toLocaleUpperCase() let c = b.toLocaleLowerCase() console.log(b,c) //分隔为数组,首字母大写 let arr = a.split(' ') let arr1 = arr.reduce((total,item)=>{ let str = item.toLocaleLowerCase() str1 = str.replace(str[0],str[0].toLocaleUpperCase()) total.push(str1) return total },[]) console.log(arr,arr1) -
字符串分割为数组
let aa = '12345' let bb = aa.split('') console.log(bb) -
多个字符串连接
let a = '1' let b = '2' let d = '3' let c = a.concat(b,d) console.log(c) -
字符串截取
let a = '1234567890' let b = a.slice(1,2) //截取索引1~2之间的数据(不含endindex);2 let c = a.substring(1,3) //截取索引1~3之间的数据(不含endindex);23 let e = a.substr(2,4) // 截取索引2开始的后4位数据 ;3456 -
字符串匹配指定值
let a = '123abcdABCD' let str = 'abc' let reg = /abc/ig console.log(a.includes(str)) //返回布尔值 let arr = a.match(str) //返回数组 let arr1 = a.match(reg) //返回数组 let sea = a.search(reg) //正则表达式,匹配成功返回索引,无匹配返回-1 console.log(arr,arr1,sea) -
字符串替换指定值
let a = '123abcdABCD' let reg = /abc/ig let b = a.replace('abc','hi') //全局匹配,区分大小写 let f = a.replace(reg,'hi') //全局匹配,不区分大小写 console.log(b,f)
2.数值(number)
-
数值保留两位小数
let num = 12.345678 let num1 = Number(num.toFixed(2)) //数据改变为字符串类型 console.log(num1)
-
数值取整(四舍五入、向上舍入、向下舍入)
let num = 12.345678 let a = Math.round(num) //四舍五入 let b = Math.ceil(num) //向上舍入 let c = Math.floor(num) // 向下舍入 let e = Math.trunc(num) //es6新增,去除小数返回整数 console.log(a,b,c) let d = parseInt(num) //解析字符串,返回整数 let f = parseFloat(num) //解析字符串,返回浮点数(只返回首个数字) console.log(d,f) -
数值字符串转化
let num = 123 let str = num.toString() let s = num+'' let ss = Number(s) console.log(str,s,ss) -
数值对比返回最大、最小值
let num = 123 ; let num1 = 234; console.log(Math.min(num,num1)) console.log(Math.max(num,num1)) -
判断数值值类型(正数、负数、零、其他)
let a = -1; let b = 0 let c = 2 let d = 'a' console.log(Math.sign(a),Math.sign(b),Math.sign(c),Math.sign(d)) //正数返回1,负数返回-1,零返回0,非数值返回NAN(非数值情况先转化数值再进行判断)
**3.日期格式数据(Date)
**
-
获取当前时间,日期和时间戳互转;
let now = new Date() //当前时间 let old = new Date('2017/08/07 08:00:00') let nowTime = now.getTime() //时间转为时间戳 let oldTime = old.getTime()
let time = new Date(oldTime).toLocaleDateString() //转为日期年月日,无时分秒 console.log(now,nowTime,old,oldTime,time) //setTime()方法通过在1970年1月1日午夜加/减指定的毫秒数来修改date显示的日期和时间let date = new Date() let time1 = date.setTime(oldTime) console.log(date)
-
获取特定日期的年、月、日、星期
let time = '2017/08/07 08:00:00' let date = new Date(time) let year = date.getFullYear() let month = date.getMonth()+1 let day = date.getDate() let week = date.getDay() console.log(year,month,day,week) -
计算两个日期之间的差值
let time = new Date('2017/08/07 08:00:00').getTime() let time1 = new Date('2017/08/08 08:00:00').getTime() console.log(time1 - time)
4.数组(array)
-
数组遍历
for...of // es6 遍历数组,for(let i of arr),i为数组内的值(值可以为对象) for...in //常用于遍历数组或者对象的属性 for 循环 Array.forEach() Array.map() Array.reduce() Array.filter()
-
数组找索引
Array.indexOf() //第一个索引 Array.lastIndexOf() //最后一个索引 Array.findIndex()//第一个索引 Array.find() // 查看数组中的特定对象 -
数组增删改查
Array.push() Array.unshift() Array.pop() Array.shift() Array.splice(索引,数量,...增加的值) Array.slice(startInd,endInd) // 截图数据,不含endInd -
数组转字符串
Array.toString() Arrry.join() -
数组排序
Array.reverse() //里面可以设置方法 -
其他
Array.some() //判断是否包含特定值,返回布尔值 Array.every() //判断是否全部包含特定值,返回布尔值 Array.form(). //类数组转化为数组,浅拷贝 Array.isArray() //判断数组数据类型
5.判断数据类型常用方法
//typeof 用于判断基本数据类型
let a = 'a'
console.log(typeof a)
//instanceof 用于判断引用数据的类型
let lili ={age:18}
console.log(lili instanceof Object)
//Objecy.prototype.toString.call() 判断引用数据的原型
let date = new Date()
console.log(Object.prototype.toString.call(date))
//obj.constructor 通过对象的原型链判断
let arr = [1,2]
console.log(arr.constructor === Function)