JavaScript对于基本函数的小结

192 阅读4分钟
  • toString() ->转化为字符串
var array1 = ['1','2','3']
console.log(array1.toString(array1));//1,2,3
  • split()->字符串转化为数组
var str = '12345';
console.log(str.split(""));//[1,2,3,4,5]
  • join()->join(分隔符)
var array1 = ['1','2','3']
console.log(array1.join('|'));1|2|3
  • push()->从尾部加入
var array1 = ['1','2','3']
console.log(array1.push('4'));//1,2,3,4
  • pop()->从尾部删除
var array1 = ['1','2','3']
console.log(array1.pop());//1,2
  • unshit()->从头部加入
var array1 = ['1','2','3']
console.log(array1.unshift('0'));//0,1,2,3
  • shift()->从头部删除
var array1 = ['1','2','3']
console.log(array1.shift());2,3
  • reverse()->翻转数组
var array1 = [1,2,3]
console.log(array1.reverse());//[3,2,1]
  • sort()->排序
var array1 = [1,3,2,8,5,6,15]
console.log(
    array1.sort((a,b) => {
        return a - b //从小到大 [1, 2, 3, 5, 6, 8, 15]
        return b - a //从大到小 [15, 8, 6, 5, 3, 2, 1]
    })
)
  • concat()->连接数组
//array1.concat(array2,array3)
var array1 = [1,2,3]
var array2 = [2,3,4]
console.log(array1.concat('wdnmd',array2));//[1,2,3,'wdnmd',2,3,4]
  • slice()->利用当前数组创建新的数组
slice(包括start.index,不包括end.index) end.index可选
var array1 = [1,2,3,4,5]
console.log(array1.slice(1));//[2,3,4,5]
console.log(array1.slice(1,4));//[2,3,4]
  • splice()->对数组进行删除/插入/替换
删除:splice(start.index,删除的项数)
插入:splice(start.index,0(删除的项数),插入的任意数量项)
替换:splice(start.index,删除的项数,插入的任意数量项)

var array1 = [1,2,3,4,5]
var removed = array1.splice(0,2)
console.log(array1);//原数组修改后的结果 [3,4,5]
console.log(removed);//返回删除的项 [1,2]

var array1 = [1,2,3,4,5]
var removed = array1.splice(1,0,2,2,2)
console.log(array1);//原数组修改后的结果 [1,2,2,2,2,3,4,5]
console.log(removed);//返回一个空数组

var array1 = [1,2,3,4,5]
var removed = array1.splice(1,2,2,4,5)
console.log(array1);//原数组修改后的结果 [1,2,4,5,4,5]
console.log(removed);//[2,3]
  • indexOf()->寻找
var array1 = [1,2,3,4,5,4,3,2,1]
console.log(array1.indexOf(7));//-1
console.log(array1.indexOf(4));//3  返回的是对应value的index
console.log(array1.indexOf(4,4));//5 返回的是对应index为4以后value为4的index
  • lastIndexOf() ->从尾部寻找
var array1 = [1,2,3,4,5,4,3,2,1]
console.log(array1.lastIndexOf(4));//5 返回的是从尾部开始查找对应value的index
console.log(array1,lastIndexOf(4,4));//3 返回的是从尾部开始查找对应value为4以后value为4的index
  • every()->对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true
//every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true
var number = [1,2,3,4,5,4,3,2,1]
var everyResult = number.every(function(item,index,value){
    return item >2;
})
console.log(everyResult);//false
console.log(number.every(item => item > 2));//false
  • some()->对数组中的每一项运行给定函数,返回该函数对任一项返回true,则返回true
//some():对数组中的每一项运行给定函数,返回该函数对任一项返回true,则返回true
var number = [1,2,3,4,5,4,3,2,1]
console.log(number.some(item => item > 4));//true
  • filter()->筛选数组
//filter():对数组中的每一项运行给定函数,返回该函数会返回true的项组成一个新的数组
var number = [1,2,3,4,5,4,3,2,1]
console.log(number.filter(item => item > 2));//[3,4,5,4,3]
  • map()->对数组中的每一个值进行一个操作,返回一个新的数组
var number = [1,2,3,4,5,4,3,2,1]
console.log(number.map(item => item * 2));//[2, 4, 6, 8, 10, 8, 6, 4, 2]
  • reduce()->从数组头部开始
var number = [1,2,3,4,5]
var sum = number.reduce(function(pre,cur,index,array){
    return pre + cur
})
console.log(sum);//15
  • reduceRight()->从数组尾部开始
var number = [1,2,3,4,5]
console.log(number.reduceRight((pre,cur) => pre + cur));//15
  • forEach()->遍历
//forEach():对数组中的每一项运行给定函数。无返回值
var number = [1,2,3,4,5,4,3,2,1]
number.forEach(function(item,index,array){
    //执行操作
})
  • call()->call()方法调用一个对象,可以改变函数的this指向(经常做继承)

  • apply()->apply()调用函数,也可以改变函数的this指向

fun.apply(thisArg,[argsArray])


thisAry: 在fun函数运行时指定的this
argsArray: 传递的值,必须包含在数组里面
返回值就是函数的返回值,因为他就是调用函数
  • bind()->bind()方法不会调用函数,但是也能改变函数this的指向
fun.bind(thisArg,arg1,arg2,...)
thisArg:在fun函数运行时指定的this
arg1,arg2:传递的其他参数
返回由指定的this值和初始化参数改造的原函数拷贝

  • from()->将伪数组或可遍历对象转化为真正的数组
let arrayLike = {
    '1':'b',
    '2':'c',
    length:3
};
let arr2 = Array.from(arrayLike);//['b','c']

let arr3 = Array.from(arrayLick,item => item*2);//['2','4']
  • find()->用于找到第一个符合条件的数组成员,如果没有找到返回undefined
let arry = [1,2,3,4,5]
let target = arry.find((item,index) => item == 2);//2返回的是一个元素
  • findIndex()->用于找到第一个符合条件的数组成员变量的位置,如果没有找到返回-1
let arry = [1,2,3,4,5]
let index = arry.findIndex((value,index) => value>2);
console.log(idnex);//2
  • includes()->数组是否包含对应的value、返回布尔值
[1,2,3].includes(2);//true
[1,2,3,4].includes(5);//false