js方法总结
toFixed()
用法:toFixed() 方法可把 Number 四舍五入为指定小数位数的数字
语法:NumberObject.toFixed(num)
let num = new Number(23.83);
// 23.8
console.log(num)
charAt()
作用:charAt() 方法可返回指定位置的字符,返回的字符是长度为 1 的字符串
语法:stringObject.charAt(index)
let str = "spring";
let strChar= str.charAt(1) ;
// 1
console.log(strChar)
concat()
作用:用于连接两个或多个数组,该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本
语法:arrayObject.concat(arrayX,arrayX,……,arrayX)
let a= [1,2,3];
let strChar= a.concat(4,5,'6') ;
// [1, 2, 3, 4, 5, "6"]
console.log(strChar)
reverse()
作用:reverse() 方法用于颠倒数组中元素的顺序
语法:arrayObject.reverse()
let a= new Array();
a[0] = 1
a[1] = "2"
let strChar= a.reverse() ;
// ["2",1]
console.log(strChar)
slice()
作用:slice() 方法可从已有的数组中返回选定的元素
语法:arrayObject.slice(start,end)
参数1:必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。
参数2:可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。
返回值:返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素
注意1:请注意,该方法并不会修改数组,而是返回一个子数组。如果想删除数组中的一段元素,应该使用方法 Array.splice()
注意2:如果 end 未被规定,那么 slice() 方法会选取从 start 到数组结尾的所有元素
search()
作用:search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串
语法:stringObject.search(regexp)
参数:该参数可以是需要在 stringObject 中检索的子串,也可以是需要检索的 RegExp 对象。注释:要执行忽略大小写的检索,请追加标志 i
返回值:stringObject 中第一个与 regexp 相匹配的子串的起始位置
注意1:如果没有找到任何匹配的子串,则返回 -1
注意2:search() 方法不执行全局匹配,它将忽略标志 g。并且总是从字符串的开始进行检索,这意味着它总是返回 stringObject 的第一个匹配的位置
splice()
用法:splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目
注:1:该方法会改变原始数组
语法:arrayObject.splice(index,howmany,item1,……,itemX)
参数1:必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。
参数2:必需。要删除的项目数量。如果设置为 0,则不会删除项目。
参数3:可选。向数组内所添加的新项目,和是否删除数组内原来的项目无关
返回值:包含被删除项目组成的新数组,如果有的话。
注意2:splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组
注意3:请注意,splice() 方法与 slice() 方法的作用是不同的,splice() 方法会直接对数组进行修改。
pop
功能:删除数组的最后一位,并且返回删除的数据,会改变原来的数组
var str1 = [12,2,"hello"];
console.log(str1.pop() //hello
console.log(str1); //[12, 2]
shift
功能:删除数组的第一位数据,并且返回新数组的长度,会改变原来的数组
var str1 = [12,2,"hello"];
console.log(str1.shift()); //12
console.log(str1); //[2,"hello"]
push
功能:在数组的最后一位新增一个或多个数据,并且返回新数组的长度,会改变原来的数组
注意:push()方法返回的是数据是新数组的长度,它增加的数据可以是一个也可以是多个,可以理解为增加一连串的数据
var str1 = [12,2,"hello"];
var str2 = [43,2,"test"];
console.log(str1.push("你好")); //4
console.log(str2.push("hello","world")); //5
console.log(str1); //[12, 2, "hello","你好"]
console.log(str2); //[43, 2, "test","hello", "world"]
forEach
功能:ES5新增的方法,用来遍历数组,没有返回值,
参数:forEach(callback);callback默认有三个参数,分别为value(遍历到的数组的数据),index(对应的索引),self(数组自身)。
var arr = ["Tom","Jack","Lucy","Lily","May"];
var a = arr.forEach(function(value,index,self){
console.log(value + "--" + index + "--" + (arr === self));
})
// 打印结果为:
// Tom--0--true
// Jack--1--true
// Lucy--2--true
// Lily--3--true
// May--4--true
console.log(a); //undefined---forEach没有返回值
//该方法为遍历方法,不会修改原数组
let array = []
let pid = 0
array.forEach((item,index)=>{
console.log(item)
if(item.id== pid ){
array.splice(index,1) //删除
array.push(pid ) //增加
array[index] = pid //修改
console.log(array[index])//打印
}
})
map
功能:
1.同forEach功能;
2.map的回调函数会将执行结果返回,最后map将所有回调函数的返回值组成新数组返回。
参数:map(callback);callback默认有三个参数,分别为value,index,self。跟上面的forEach()的参数一样
//功能1:同forEach
var arr = ["Tom","Jack","Lucy","Lily","May"];
var a = arr.map(function(value,index,self){
console.log(value + "--" + index + "--" + (arr === self))
})
// 打印结果为:
// Tom--0--true
// Jack--1--true
// Lucy--2--true
// Lily--3--true
// May--4--true
//功能2:每次回调函数的返回值被map组成新数组返回
var arr = ["Tom","Jack","Lucy","Lily","May"];
var a = arr.map(function(value,index,self){
return "hi:"+value;
})
console.log(a); //["hi:Tom", "hi:Jack", "hi:Lucy", "hi:Lily", "hi:May"]
console.log(arr); //["Tom", "Jack", "Lucy", "Lily", "May"]---原数组未改变
filter
功能:
1.同forEach功能;
2.filter的回调函数需要返回布尔值,当为true时,将本次数组的数据返回给filter,最后filter将所有回调函数的返回值组成新数组返回(此功能可理解为“过滤”)。
参数:filter(callback);callback默认有三个参数,分别为value,index,self。
//功能1:同forEach
var arr = ["Tom","Jack","Lucy","Lily","May"];
var a = arr.filter(function(value,index,self){
console.log(value + "--" + index + "--" + (arr === self))
})
// 打印结果为:
// Tom--0--true
// Jack--1--true
// Lucy--2--true
// Lily--3--true
// May--4--true
//功能2:当回调函数的返回值为true时,本次的数组值返回给filter,被filter组成新数组返回
var arr = ["Tom","Jack","Lucy","Lily","May"];
var a = arr.filter(function(value,index,self){
return value.length > 3;
})
console.log(a); //["Jack", "Lucy", "Lily"]
console.log(arr); //["Tom", "Jack", "Lucy", "Lily", "May"]---原数组未改变
ever
功能:判断数组中每一项是否都满足条件,只有所有项都满足条件,才会返回true。
参数:every()接收一个回调函数作为参数,这个回调函数需要有返回值,every(callback);callback默认有三个参数,分别为value,index,self。
功能1:当回调函数的返回值为true时,类似于forEach的功能,遍历所有;如果为false,那么停止执行,后面的数据不再遍历,停在第一个返回false的位置。
功能2:当每个回调函数的返回值都为true时,every的返回值为true,只要有一个回调函数的返回值为false,every的返回值都为false
复制代码
//demo1:
var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
var a = arr.every(function(value,index,self){
return value.length > 3;
})
console.log(a); //false
//demo2:
var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
var a = arr.every(function(value,index,self){
return value.length > 2;
})
console.log(a); //true
some
功能:判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。
参数:some()接收一个回调函数作为参数,这个回调函数需要有返回值,some(callback);callback默认有三个参数,分别为value,index,self。
功能1:因为要判断数组中的每一项,只要有一个回调函数返回true,some都会返回true,所以与every正好相反,当遇到一个回调函数的返回值为true时,可以确定结果,那么停止执行,后面都数据不再遍历,停在第一个返回true的位置;当回调函数的返回值为false时,需要继续向后执行,到最后才能确定结果,所以会遍历所有数据,实现类似于forEach的功能,遍历所有。
功能2:与every相反,只要有一个回调函数的返回值都为true,some的返回值为true,所有回调函数的返回值为false,some的返回值才为false
//demo1:
var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
var a = arr.some(function(value,index,self){
return value.length > 3;
})
console.log(a); //true
//demo2:
var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
var a = arr.some(function(value,index,self){
return value.length > 4;
})
console.log(a); //false
setInterval
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。
setInterval(function(){ alert("Hello"); }, 3000);
setInterval('alert("Hello");', 3000);
setInterval(this.rotator, 3000);
// 文字轮播刷新数组方法
rotator() {
this.animate =true
setTimeout (() => {
this.marqueeList.push (this.marqueeList[ 0 ]);
this.marqueeList.shift ();
this.animate = false;
}, 500);
},