concat()
连接两个或更多的数组,并返回结果let arr1 = [1,2,3];
let arr2 = [4,5,6];
let arr3 = arr1.concat(arr2);
console.log(arr3)
indexOf(要查找的元素)
搜索数组中的元素,并返回查找元素所在的位置let arr = [1,2,3,4,5];
let index = arr.indexOf(4);
console.log(index)
join(指定字符)
把数组的所有元素用指定字符进行拼接let arr = ['a','b','c','d','e'];
let str = arr.join('');
console.log(str)
lastIndexOf(要查找的元素)
搜索数组中的元素,并返回要查找元素最后出现的位置。let arr = [1,2,3,4,1,5];
let index = arr.lastIndexOf(1);
console.log(index)
pop()
删除数组的最后一个元素并返回删除的元素。原数组改变let arr = ['a','b','c','d','e'];
let res = arr.pop();
console.log(res)
console.log(arr)
push()
向数组的末尾添加一个或更多元素,并返回添加后数组的长度。原数组改变let arr = ['a','b','c','d'];
let len = arr.push('e');
console.log(len)
console.log(arr)
shift()
: 删除数组中第一个元素,并返回删除的元素。原数组改变let arr = ['a','b','c','d','e'];
let res = arr.shift();
console.log(res);
console.log(arr)
unshift(要添加的元素)
向数组的起始位置添加一个或更多元素,并返回添加后数组的长度。原数组改变let arr = ['b','c','d','e'];
let len = arr.unshift('a');
console.log(len);
console.log(arr);
slice(起始位置,结束位置)
选取数组的的一部分,并返回一个新数组。let arr = ['a','b','c','d','e'];
let arr1 = arr.slice(0,3);
console.log(arr1);
let arr2 = arr.slice(1);
console.log(arr2);
let arr3 = arr.slice(0,-1);
console.log(arr3);
let arr4 = arr.slice(-2);
console.log(arr4);
splice()
从数组中添加或删除元素。
let arr = [10,20,30,40,50,60,70,80,90]
let res = arr.splice(2,4)
console.log(res,arr)
let arr = [10,20,30,40,50];
let res = arr.splice(1,2,'李四','张三');
console.log(res,arr);
let arr = [10,20,30,40,50,60,70,80,90]
arr.splice(3,0,'呵呵呵');
console.log(arr);
forEach()
循环遍历数组。let arr = ['a','b','c'];
arr.forEach((item,index,arr)=>{
console.log(item,index,arr)
})
isArray()
判断对象是否为数组。返回布尔值let str = '123';
console.log(Array.isArray(str));
reduce()
将数组元素计算为一个值(从左到右)。let arr = [1,2,3,4,5];
let res = arr.reduce((a,b)=>{
return a+b
})
console.log(res)
reduceRight()
将数组元素计算为一个值(从右到左)。reverse()
反转数组的元素顺序。let arr = ['a','b','c','d'];
arr.reverse();
console.log(arr)
every()
检测数值元素的每个元素是否都符合条件。返回布尔值let arr = [70,60,45,80,59];
let flag = arr.every(item=>item>=60);
console.log(flag)
详见:JavaScript中Array数组的every()函数的详解some()
检测数组元素中是否有元素符合指定条件。let arr = [70,60,45,80,59];
let flag = arr.some(item=>item==80);
console.log(flag)
详见:Array.some() 方法map()
通过指定函数处理数组的每个元素,并返回处理后的数组。let arr = [70,60,45,80,59];
let newArr = arr.map(item=>item+1);
console.log(newArr)
详见:JS语法之:map()方法filter()
检测数值元素,并返回符合条件所有元素的数组。let arr = [34,21,44,19];
let newArr = arr.filter(item=>item>25);
console.log(newArr)
详见:Array.filter()解析includes()
判断一个数组是否包含一个指定的值。返回布尔值let arr = ['a','b','c','d'];
let flag = arr.includes('e');
console.log(flag)
详见:ES6之Array.includes()函数fill()
使用一个固定值来填充数组。
let arr = ['a','b','c','d','e'];
let newArr = arr.fill('*',0,2);
console.log(newArr)
详见:ES6的Array.fill()方法copyWithin()
从数组的指定位置拷贝元素到数组的另一个指定位置中。返回修改后的数组(即直接修改原数组),不会改变数组的长度
var arr = ["apple","banana","cover","double"];
arr.copyWithin(1,1,2)
console.log(arr)
详见:ES6中Array.copyWithin()函数的用法实例详解find()
返回符合传入测试(函数)条件的数组元素。如果找不到则返回undefined
let arr = ['a','b','c','d','e'];
let res = arr.find(item=>item=='c');
console.log(res)
findIndex()
返回符合传入测试(函数)条件的数组元素索引。找不到则返回-1
let arr = ['a','b','c','d','e'];
let res = arr.findIndex(item=>item=='c');
console.log(res)
from()
将一个类数组对象或者可遍历对象转换成一个真正的数组。
let arrayLike = {
0: 'tom',
1: '65',
2: '男',
3: ['jane','john','Mary'],
'length': 4
}
let arr = Array.from(arrayLike);
console.log(arr)
如果将上面代码中的length属性去掉,将会得到一个长度为0的空数组;如果将上面的属性名不为数字类型,改为字符串,将会得到一个数组元素全为`undefined`的数组。所以要想将类数组转换为真正的数组必须具备以下条件:
1. 该类数组对象必须具有length属性,用于指定数组的长度。如果没有length属性,那么转换后的数组是一个空数组。
2. 该类数组对象的属性名必须为数值型或字符串型的数字
PS:该类数组对象的属性名可以加引号,也可以不加引号
let arr = [12,45,97,9797,564,134,45642]
let set = new Set(arr)
console.log(Array.from(set))
Array.from()
还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。如下:let arr = [12,45,97,9797,564,134,45642]
let set = new Set(arr)
console.log(Array.from(set, item => item + 1))
let str = 'hello world!';
console.log(Array.from(str))
- Array.from参数是一个真正的数组。
Array.from
会返回一个一模一样的新数组
sort()
对数组的元素进行排序。
var arr=[1,3,10,4,2];
arr.sort();
console.log(arr);
var arr=[1,3,10,4,2];
function compare(value1,value2) {
if(value1<value2){return -1;}
else if(value1>value2){return 1;}
else {return 0}
}
arr.sort(compare);
console.log(arr)
详见:array.sort()方法及原理keys()
返回带有数组键的 Array Iterator 对象。let arr = ['a','b','c','d'];
let keys = arr.keys();
for(let i of keys){
console.log(i)
}
详见:你真的了解Array.keys的用法吗toString()
把数组转换为字符串,并返回结果。let arr = [1,2,3,4];
let res = arr.toString();
console.log(res)
valueOf()
返回数组对象的原始值。entries()
返回带有数组键的 Array Iterator 对象,但是与keys()
有所不同let arr = ['a','b','c','d'];
let res = arr.entries();
for(let a of res){
console.log(a)
}
charAt(index)
返回在指定位置的字符let str = 'abcde';
let a = str.charAt(3);
console.log(a)
concat()
拼接let str1 = 'abc';
let str2 = 'def';
let str3 = str1.concat(str2);
console.log(str3)
indexOf()
返回字符在字符串中的首次出现的位置,如果找不到则返回-1
let str = 'abcdeabcde';
let index = str.indexOf('c');
console.log(index)
includes()
查找在字符串中是否包含某个字符,有则返回true
,没有返回false
let str = 'abcde';
let res = str.includes('b');
console.log(res)
lastIndexOf()
返回字符在字符串中最后出现的位置,如果找不到则返回-1
let str = 'abcdeabcde';
let index = str.lastIndexOf('c');
console.log(index)
slice(start,end)
截取指定位置的字符并返回let str = 'abcdefg';
let res = str.slice(1,3);
console.log(res)
split()
将字符串按指定字符分割为数组
let str = '1-2-3-4-5';
let res = str.split('-');
let res2 = str.split('-',2);
console.log(res)
console.log(res2)
substr(start,length)
返回截取后的字符串let str = 'abcde';
let res = str.substr(1,2);
console.log(res)
substring(start,end)
返回截取后的字符串let str = 'abcdef';
let res = str.substring(1,4);
console.log(res)
toLowerCase()
将字母转换为小写let str = 'ABCDE';
let res = str.toLowerCase();
console.log(res)
toUpperCase()
将字母转换为大写let str = 'abcde';
let res = str.toUpperCase();
console.log(res)
trim()
去除字符串两边的空白let str = ' abcde ';
let res = str.trim();
console.log(res)
replace(正则表达式或指定字符,要替换的内容)
返回替换后的字符串。如果要替换的字符不止一处,则只替换第一处。let str = 'ab9cde2f';
let res = str.replace('c','G');
let res2 = str.replace(/[0-9]/,'Z');
console.log(res)
console.log(res2)
replaceAll(正则表达式或指定字符,要替换的内容)
返回替换后的字符串。与replace
不同的是替换所有位置的字符let str = 'ab9cde2fabc';
let res = str.replaceAll('c','G');
let res2 = str.replaceAll(/[0-9]/g,'Z');
console.log(res)
console.log(res2)
charCodeAt()
返回指定位置字符的Unicode码let str = '四';
let res = str.charCodeAt();
console.log(res)
fromCharCode()
将Unicode码转换为字符let res = String.fromCharCode(22235)
console.log(res)
repeat()
复制字符串指定次数,并将它们连接在一起返回let str = 'abc';
let res = str.repeat(2);
console.log(res)
startsWith()
查看字符串是否以指定的子字符串开头,是则返回true
,不是则返回false
let str = 'abc';
let res = str.startsWith('a');
console.log(res)
toLocaleLowerCase()
根据本地主机的语言环境把字符串转换为小写let str = 'ABC';
let res = str.toLocaleLowerCase();
console.log(res)
toLocaleUpperCase()
根据本地主机的语言环境把字符串转换为大写let str = 'abc';
let res = str.toLocaleUpperCase();
console.log(res)
valueOf()
返回某个字符串对象的原始值toString()
返回一个字符串
getFullYear()
从 Date 对象以四位数字返回当前年份let date = new Date();
console.log(date.getFullYear())
getMonth()
从 Date 对象返回当前月份 (0 ~ 11)。要注意的是这里获取的月份与实际的月份差1
个月,所以当前月份 = 获取月份 + 1
let date = new Date();
console.log(date.getMonth())
getDay()
从 Date 对象返回一周中的某一天(0 ~ 6)。因为这里是按照国外的标准进行判定,所以0
对应的是国外的周日。let date = new Date();
console.log(date.getDay())
getDate()
从 Date 对象返回一个月中的某一天let date = new Date();
console.log(date.getDate())
getHours()
返回 Date 对象的小时let date = new Date();
console.log(date.getHours())
getMinutes()
返回 Date 对象的分钟 (0 ~ 59)let date = new Date();
console.log(date.getMinutes())
getSeconds()
返回 Date 对象的秒数 (0 ~ 59)let date = new Date();
console.log(date.getSeconds())
getMilliseconds()
返回 Date 对象的毫秒(0 ~ 999)let date = new Date();
console.log(date.getMilliseconds())
getTime()
返回 1970 年 1 月 1 日至今的毫秒数(时间戳)let date = new Date();
console.log(date.getTime())
getTimezoneOffset()
返回本地时间与格林威治标准时间 (GMT) 的分钟差let date = new Date();
console.log(date.getTimezoneOffset())
getYear()
已废弃。 请使用 getFullYear() 方法代替parse()
返回1970年1月1日午夜到指定日期(字符串)的毫秒数let date = new Date();
console.log(Date.parse(date))
setFullYear()
设置 Date 对象中的年份(四位数字),返回所设置年份的毫秒数(时间戳)let date = new Date();
console.log(date.setFullYear(2021))
setMonth()
设置 Date 对象中月份 (0 ~ 11),返回当前年份所设置月份的毫秒数let date = new Date();
console.log(date.setMonth(3))
setDate()
设置 Date 对象中月的某一天,返回当前年当前月所设置天数的毫秒数let date = new Date();
console.log(date.setDate(14))
setHours()
设置 Date 对象中的小时 (0 ~ 23)let date = new Date();
console.log(date.setHours(14))
setMinutes()
设置 Date 对象中的分钟 (0 ~ 59)let date = new Date();
console.log(date.setMinutes(14))
setSeconds()
设置 Date 对象中的秒钟 (0 ~ 59)let date = new Date();
console.log(date.setSeconds(14))
setMilliseconds()
设置 Date 对象中的毫秒 (0 ~ 999)let date = new Date();
console.log(date.setMilliseconds(143))
setTime()
以毫秒设置 Date 对象,返回当前设置的时间的值let date = new Date();
console.log(date.setTime(143))
setYear()
已废弃。请使用 setFullYear() 方法代替toDateString()
把 Date 对象的日期部分转换为字符串let date = new Date();
console.log(date.toDateString())
toGMTString()
已废弃。请使用 toUTCString() 方法代替toISOString()
使用 ISO 标准返回字符串的日期格式let date = new Date();
console.log(date.toISOString())
toJSON()
以 JSON 数据格式返回日期字符串let date = new Date();
console.log(date.toJSON())
toLocaleDateString()
根据本地时间格式,把 Date 对象的日期部分转换为字符串let date = new Date()
console.log(date.toLocaleDateString())
toLocaleTimeString()
根据本地时间格式,把 Date 对象的时间部分转换为字符串let date = new Date()
console.log(date.toLocaleTimeString())
toLocaleString()
据本地时间格式,把 Date 对象转换为字符串let date = new Date()
console.log(date.toLocaleString())
toString()
把 Date 对象转换为字符串let date = new Date()
console.log(date.toString())
toUTCString()
根据世界时,把 Date 对象转换为字符串let date = new Date()
console.log(date.toUTCString())
toTimeString()
把 Date 对象的时间部分转换为字符串let date = new Date()
console.log(date.toTimeString())
valueOf()
返回 Date 对象的原始值let date = new Date()
console.log(date.valueOf())
UTC()
接受的参数同日期构造函数接受最多参数时一样,返回从1970-1-1 00:00:00 UTC到指定日期的的毫秒数。console.log(Date.UTC(2022,4,20))
getUTCDate()
根据世界时从 Date 对象返回月中的一天 (1 ~ 31)let date = new Date()
console.log(date.getUTCDate())
getUTCFullYear()
根据世界时从 Date 对象返回四位数的年份let date = new Date()
console.log(date.getUTCFullYear())
getUTCMonth()
根据世界时从 Date 对象返回月份 (0 ~ 11)let date = new Date()
console.log(date.getUTCMonth())
getUTCDay()
根据世界时从 Date 对象返回周中的一天 (0 ~ 6)let date = new Date()
console.log(date.getUTCDay())
getUTCHours()
根据世界时返回 Date 对象的小时 (0 ~ 23)let date = new Date()
console.log(date.getUTCHours())
getUTCMinutes()
根据世界时返回 Date 对象的分钟 (0 ~ 59)let date = new Date()
console.log(date.getUTCMinutes())
getUTCSeconds()
根据世界时返回 Date 对象的秒钟 (0 ~ 59)let date = new Date()
console.log(date.getUTCSeconds())
getUTCMilliseconds()
根据世界时返回 Date 对象的毫秒(0 ~ 999)let date = new Date()
console.log(date.getUTCMilliseconds())
setUTCFullYear()
根据世界时设置 Date 对象中的年份(四位数字)let date = new Date()
console.log(date.setUTCFullYear(2020))
setUTCMonth()
根据世界时设置 Date 对象中的月份 (0 ~ 11)let date = new Date()
console.log(date.setUTCMonth(3))
setUTCDate()
根据世界时设置 Date 对象中月份的一天 (1 ~ 31)let date = new Date()
console.log(date.setUTCDate(20))
setUTCHours()
根据世界时设置 Date 对象中的小时 (0 ~ 23)let date = new Date()
console.log(date.setUTCHours(22))
setUTCMinutes()
根据世界时设置 Date 对象中的分钟 (0 ~ 59)let date = new Date()
console.log(date.setUTCMinutes(22))
setUTCSeconds()
用于根据世界时 (UTC) 设置指定时间的秒字段let date = new Date()
console.log(date.setUTCSeconds(22))
setUTCMilliseconds()
根据世界时设置 Date 对象中的毫秒 (0 ~ 999)let date = new Date()
console.log(date.setUTCMilliseconds(222))
-
abs(x)
返回绝对值
console.log(Math.abs(-1))
-
acos(x)
返回一个数的反余弦值(单位为弧度)
acos
方法以 -1
到 1
的一个数为参数,返回一个 0
到 π
(弧度)的数值。如果传入的参数值超出了限定的范围,将返回 NaN
。
Math.acos(-2);
Math.acos(-1);
Math.acos(0);
Math.acos(0.5);
Math.acos(1);
Math.acos(2);
-
asin(x)
返回一个数值的反正弦(单位为弧度)
asin
方法接受 -1 到 1 之间的数值作为参数,返回一个介于-π/2
到π/2
弧度的数值。如果接受的参数值超出范围,则返回 NaN
。
Math.asin(-2);
Math.asin(-1);
Math.asin(0);
Math.asin(0.5);
Math.asin(1);
Math.asin(2);
-
atan(x)
返回一个数值的反正切(以弧度为单位)
atan
返回一个 -π/2
到 π/2
弧度之间的数值。
Math.atan(1);
Math.atan(0);
-
atan2(y,x)
返回从原点(0,0)到(x,y)点的线段与x轴正方向之间的平面角度(弧度值),也就是Math.atan2(y,x)
atan2
方法返回一个 -π
到 π
之间的数值,表示点 (x, y) 对应的偏移角度。这是一个逆时针角度,以弧度为单位,正X轴和点 (x, y) 与原点连线 之间。注意此函数接受的参数:先传递 y 坐标,然后是 x 坐标。
atan2
接受单独的 x 和 y 参数,而 atan
接受两个参数的比值。
Math.atan2(90, 15)
Math.atan2(15, 90)
Math.atan2( ±0, -0 )
Math.atan2( ±0, +0 )
Math.atan2( ±0, -x )
Math.atan2( ±0, x )
Math.atan2( -y, ±0 )
Math.atan2( y, ±0 )
Math.atan2( ±y, -Infinity )
Math.atan2( ±y, +Infinity )
Math.atan2( ±Infinity, x )
Math.atan2( ±Infinity, -Infinity )
Math.atan2( ±Infinity, +Infinity )
-
ceil(x)
上舍入,返回大于或等于一个给定数字的最小整数。
console.log(Math.ceil(.95));
console.log(Math.ceil(4));
console.log(Math.ceil(7.004));
console.log(Math.ceil(-7.004));
-
cos(x)
返回一个数值的余弦值。
cos
方法返回一个 -1
到 1
之间的数值,表示角度(单位:弧度)的余弦值。
Math.cos(0);
Math.cos(1);
Math.cos(Math.PI);
Math.cos(2 * Math.PI);
-
exp(x)
函数返回 e^x
,x
表示参数,e
是欧拉常数,自然对数的底数
Math.exp(-1);
Math.exp(0);
Math.exp(1);
-
floor(x)
下舍入,返回小于或等于一个给定数字的最大整数。
Math.floor( 45.95);
Math.floor( 45.05);
Math.floor( 4 );
Math.floor(-45.05);
Math.floor(-45.95);
-
log(x)
返回一个数的自然对数
如果指定的 number
为负数,则返回值为 NaN
。
Math.log(-1);
Math.log(0);
Math.log(1);
Math.log(10);
-
max()
返回一组数中的最大值。
console.log(Math.max(1, 3, 2));
console.log(Math.max(-1, -3, -2));
const array1 = [1, 3, 2];
console.log(Math.max(...array1));
-
min()
返回零个或更多个数值的最小值。如果任一参数不能转换为数值,则返回NaN
。如果没有参数,结果为Infinity
。
Math.min(10,20,5);
-
pow(x,y)
返回基数(base
)的指数(exponent
)次幂,即 base^exponent
。
console.log(Math.pow(7, 3));
console.log(Math.pow(4, 0.5));
console.log(Math.pow(7, -2));
console.log(Math.pow(-7, 0.5));
-
random()
函数返回一个浮点数, 伪随机数在范围从0
到小于1
,也就是说,从0(包括0)往上,但是不包括1(排除1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
console.log(getRandomInt(3));
console.log(getRandomInt(1));
console.log(Math.random());
-
round()
返回一个数字四舍五入后最接近的整数。
如果参数的小数部分大于 0.5,则舍入到相邻的绝对值更大的整数。 如果参数的小数部分小于 0.5,则舍入到相邻的绝对值更小的整数。如果参数的小数部分恰好等于0.5,则舍入到相邻的在正无穷(+∞)方向上的整数。注意,与很多其他语言中的round()函数
不同,Math.round()并不总是舍入到远离0的方向(尤其是在负数的小数部分恰好等于0.5的情况下)
。
x = Math.round(20.49);
x = Math.round(20.5);
x = Math.round(-20.5);
x = Math.round(-20.51);
-
sin()
返回一个数值的正弦值。
sin
方法返回一个 -1
到 1
之间的数值,表示给定角度(单位:弧度)的正弦值。
Math.sin(0);
Math.sin(1);
Math.sin(Math.PI / 2);
-
sqrt()
返回一个数的平方根。如果参数 number
为负值,则 sqrt 返回NaN
。
Math.sqrt(9);
Math.sqrt(2);
Math.sqrt(1);
Math.sqrt(0);
Math.sqrt(-1);
Math.sqrt(-0);