Math对象
MDN文档地址:developer.mozilla.org/zh-CN/docs/…
Math.max() /Math.min() 最大值/最小值
自己封装对象求最大值/最小值
var myMath = {
max:function(){
var max = arguments[0];
for (var i = i;i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
},
min:function(){
var min = arguments[0];
for (var i = i;i < arguments.length; i++) {
if (arguments[i] < min) {
min = arguments[i];
}
}
return min;
},
}
Math.abs() 绝对值
Math.abs(1);
cosole.log(Math.abs(1)); //返回1
Math.abs(-1);
cosole.log(Math.abs(-1)); //返回1
三种取整方法
Math.floor() //向下取整
Math.floor(1.1)//1
Math.floor(1.9)//1
Math.ceil() //向上取整
Math.ceil(1.1) //2
Math.ceil(1.9) //2
Math.round() //四舍五入
Math.round(1.1) //1
Math.round(1.9) //2
Math.round(-1.5) // -1 特殊情况,.5往大了取
随机数random()
作用:返回一个随机的小数,范围 [0,1)
例子:两个数之间的随机整数,包括两个数之间
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
日期对象
MDN文档:developer.mozilla.org/zh-CN/docs/…
创建一个新Date对象的唯一方法是通过new操作符,例如:let now = new Date();若将它作为常规函数调用(即不加new操作符),将返回一个字符串,而非Date对象。
var date = new Date(); //返回当前时间
//参数常用的写法:
var date1 = new Date(2019,10,1)
var date2 = new Date("2019-10-1") //最常用
日期格式化
var date = new Date('2023-1-19');
console.log(date.getFullYear());//返回当前日期的年
var date = new Date('2019-1-19');
console.log(date.getMonth()+1); // 月份返回小1,记得要+1
date.getDate()//返回的是几号
date.gerDay() //周日返回的是0
date.getHours() //时
date.getMinutes() //分
date.getSeconds() //秒
//封装一个函数返回当前时分秒
function getTimer() {
var time = new Date();
var h = time.getHours();
h=h<10?'0'+h:h;
}
//获取日期的总的毫秒数形式(时间戳) 4种写法 初始时间1970年1月1日
var date = new Date();
date.valueOf();//距离1970年过了多少毫秒
date.getTime();//
最常用写法:
var date = +new Date();//返回当前毫秒数
H5新增
Date.now()
倒计时案例
function conutDown(time) {
var nowTime = +new Date()//返回当前时间总毫秒数
var inputTime = +new Date(time); //返回的是用户输入时间总的毫秒数
var times = (inputTime - nowTime) / 1000; //time是剩余时间总的秒数
//进行转换
var d = parseInt(times / 60 / 60 / 24);//天
var h = parseInt(times / 60 / 60 % 24);//时
var m = parseInt(times / 60 % 60);//分
var s = parseInt(times % 60); 当前的秒
}
数组对象
MDN文档:developer.mozilla.org/zh-CN/docs/…
创建数组的两种方式
1、利用字面量
var arr = [1,2,3];
2、利用new Array()
var arr = new Array() //创建一个空的数组
var arr = new Array(2) //这个2表示数组长度为2,里面有两个空的数组元素
var arr = new Array(2,3) //这样写表示里面有两个数组元素是2和3
检测是否为数组
(1)运算符方式
instanceof // 运算符 用来检测是否为数组
var arr = [];
console.log(arr instanceof Array); //返回true
(2)内置对象
Array.isArray(参数)
添加数组元素
(1)末尾添加元素
push() //在我们数组的末尾 添加一个或者多个数组元素
var arr = [1,2,3];
arr.push(4);
arr.push(1,3,"Tom");
(2)开头添加元素
var arr = [1,2,3];
arr.unshift();
arr.unshift(4);
arr.unshift(1,3,"Tom");
删除数组元素
(1)pop()
var arr = [1,2,3];
arr.pop(); //可以删除数组的最后一个元素,不需要传参数
//返回值为数组的最后一个元素
(2)shift
var arr = [1,2,3];
arr.shift() //删除数组的第一个元素,不需要传参数,返回值为数组的第一个元素
案例:筛选数组
有一个包含工资的数组[1500,2000,1200,2100,1800],要求把数组中超过2000的删除,剩余的放到新数组中。
var arr = [1500,2000,1200,2100,1800];
var newArr = [];
for (i = 0; i < arr.length; i++) {
if (arr[i] < 2000) {
newArr.push(arr[i]);
}
数组排序
翻转数组
arr.reverse();
冒泡排序
arr.sort(function (a,b) {
// return a-b; //升序排序
// return b-a; //降序排序
});
数组索引方法
indexof
arr.indexof('数组元素') //返回数组元素的索引号
//只返回第一个满足条件的索引号
//找不到元素时返回-1
lastindexof 简单了解即可
arr.lastindexof('数组元素')//从数组后面开始查找
案例:数组去重
var arr = [1,2,2,3,3,4,4,5,6,7,8];
function unique(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (newArr.indexof(arr[i]) === -1) {
newArr.push(arr[i]);
}
}
return newArr;
}
数组转化成字符串
toString()
join(分隔符)
arr.join() //默认逗号
arr.join('-');
字符串对象
MDN地址:developer.mozilla.org/zh-CN/docs/…
根据字符返回位置
str.indexof('要查找的字符',[起始的位置])
var str = 'ABC';
str.indexof('B');
案例:返回字符位置 查找字符串'aabbiouipodffhoifnej'中所有o出现的位置
var str = 'aabbiouipodffhoifnej';
var index = str.indexof('o');
while(index != -1) {
console.log(index);
str.indexof('o',index+1);
}
根据位置返回字符(重点)
1、charAt(index)
2、charCodeAt(index) 返回相应索引号字符的ASCII码 目的:主要用来判断用户按了哪个键
3、str[index] H5新增
案例:统计出现次数最多的字符 求:字符串'aabbiouipodffhoifnej'中出现最多的字符,并统计其次数
var str = 'aabbiouipodffhoifnej';
var o = {};
for (i = 0; i < str.length; i++) {
var chars = str.At(i);
if (o[chars]) {//chars是字符串的每一个字符
o[chars]++;//o[chars] 得到的是属性值
} else {
o[chars] = 1;
}
}
var max = 0;
for (var k in o) {
if {o[k] > max} {
max = o[k];
}
}
字符串的操作方法(重点)
1、concat('str1','str2',...) 效果等于+ ,+更常用
2、substr(start,length)
var str1 = 'lalalandisgoodmovie';
str1.substr(1,2)//第一个是索引号;第二个是取几个字符;
3、替换字符串
replace('被替换的字符','替换后的字符')//只会替换第一个符合条件的字符
4、字符转换为数组 split('分隔符)