JS一些常用方法汇总

目录

第一章、数组常用方法

1.1 数组方法

1.2 总结方法是否改变原数组

第二章 、字符串常用方法

第三章、数值常用方法

第四章、Math常用方法

第五章、Data常用方法


第一章、数组常用方法

1.1 数组方法

1.toString():把数组转换为(用逗号连接的)字符串,并返回结果

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.toString()
console.log(array,array2) // ['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆'] '❆LO❆,AND,❆VE❆,❆LOVE❆'

2.join():把数组中的所有元素转换一个字符串(可带参,参数为连接的字符/字符串),默认状态下与toString()方法一样用逗号连接的字符串

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.join()
let array3 = array.join('+')
console.log(array,array2,array3)
//输出结果:['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆'] '❆LO❆,AND,❆VE❆,❆LOVE❆' '❆LO❆+AND+❆VE❆+❆LOVE❆'

3.push():向数组的末尾添加一个或更多元素,注意返回的不是数组而是新数组的长度

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.push('LOANDVE','LO')
console.log(array,array2) //['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆', 'LOANDVE', 'LO'] 6

unshift():可向数组的开头添加一个或更多元素,并返回新数组的长度

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.unshift('LOANDVE')
console.log(array,array2)//['LOANDVE', '❆LO❆', 'AND', '❆VE❆', '❆LOVE❆'] 5

4.pop():删除数组的最后一个元素,注意返回的是删除的元素

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.pop()
console.log(array,array2) //['❆LO❆', 'AND', '❆VE❆'] '❆LOVE❆'

shift():删除并返回数组的首个元素

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.shift()
console.log(array,array2) //['AND', '❆VE❆', '❆LOVE❆'] '❆LO❆'

5.slice():从已有的数组中返回选定的元素,slice(start,end)区间[start,end)的片段

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.slice(0,3)
console.log(array,array2) //['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆'] ['❆LO❆', 'AND', '❆VE❆']
  • 该方法还适用于字符串,取字符串的某个部分,并以新的字符串返回被提取的部分
  • 该方法截取的长度最长就是原数组的长度

6.splice():用于添加或删除数组中的元素,返回处理后的元素

-- 参数:splice(参数1,参数2,参数3)

        · 参数1:必选,添加/删除元素的位置(索引值,删除往后)

        · 参数2:可选,删除元素的数量

        ·  参数3:可选,需要添加什么元素

(1)删除:当只有一个参数时,删除的是,该索引以及之后的所有元素

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.splice(1) 
console.log(array,array2) //['❆LO❆'] ['AND', '❆VE❆', '❆LOVE❆']

 当有两个参数时,删除的是,从该索引开始往后数的指定个数的元素

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.splice(1,2)
console.log(array,array2) //['❆LO❆', '❆LOVE❆'] ['AND', '❆VE❆']

(2)只修改:第二个参数为0,参数3可以写多个要插入的字符串

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.splice(1,0,'LOVE','LO')
console.log(array,array2) //['❆LO❆', 'LOVE', 'LO', 'AND', '❆VE❆', '❆LOVE❆'] []

(3)删除并修改:从哪开始,删除多少个元素,再添加需要加入的一个或者多个元素

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.splice(1,2,'LOVE','LO')
console.log(array,array2) //['❆LO❆', 'LOVE', 'LO', '❆LOVE❆'] (2) ['AND', '❆VE❆']

7.concat():连接两个或更多的数组,并返回结果;语法:array1.concat(array2,..., arrayX)

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = ['LO']
let array3 = ['VE']
let array1 = array.concat(array2,array3)
let array4 = array.concat(array2)
console.log(array,array1,array4)
//输出结果:['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆']  ['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆', 'LO', 'VE']  ['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆', 'LO']

8.reverse():反转数组

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.reverse()
console.log(array,array2) //['❆LOVE❆', '❆VE❆', 'AND', '❆LO❆'] (4) ['❆LOVE❆', '❆VE❆', 'AND', '❆LO❆']
//注意原array:['❆LO❆','AND','❆VE❆','❆LOVE❆']

9.every():检测数组所有元素是否都符合指定条件(通过函数提供)

var arr = [ 1, 2, 3, 4, 5, 6 ]; 
		
console.log( arr.every( function( item, index, array ){ 
    console.log( 'item=' + item + ',index='+index+',array='+array ); 
    return item > 3; 
}));
/*
    输出结果:
    item=1,index=0,array=1,2,3,4,5,6
    false
*/
  • 该方法不会对空数组进行检测;只要检测到一个不符合要求,返回false,都符合才返回true
  • 参数: array.every(function(currentValue,index,arr), thisValue)

        -- array是需要检测的数组

        -- function(currentValue,index,arr),该方法是检测的规则方法,数组中的每个元素都会执行这个函数

            · currentValue:array当前元素的值

            · index:当前元素的索引值

            · arr:当前元素属于的数组对象

        -- thisValue:可选

some():检测数组元素是否有符合指定条件(通过函数提供)

var arr = [ 1, 2, 3, 4, 5, 6 ]; 
		
console.log( arr.some( function( item, index, array ){ 
    console.log( 'item=' + item + ',index='+index+',array='+array ); 
    return item > 3; 
})); 
/*
    输出结果:
    item=1,index=0,array=1,2,3,4,5,6
    item=2,index=1,array=1,2,3,4,5,6
    item=3,index=2,array=1,2,3,4,5,6
    item=4,index=3,array=1,2,3,4,5,6
    true
*/
  •  every与some的区别

        -- 同:

                · 都可以遍历数组中每一项的数据;

                · 都不会对空数组进行检测;

                · 都不会改变原始数组。

        -- 异:

                · every:如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测;反之返回true

                · some:如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测;如果迭代完没有满足的则返回false

10.sort():对数组的元素进行排序,默认从小到大排序

(1)直接使用sort()

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let array2 = array.sort()
console.log(array,array2) //['AND', '❆LOVE❆', '❆LO❆', '❆VE❆'] ['AND', '❆LOVE❆', '❆LO❆', '❆VE❆']
//原array数组:['❆LO❆','AND','❆VE❆','❆LOVE❆']

(2)自定义升序

var array = [ 7, 2, 9, 4, 5, 6 ]; 
let array2 = array.sort((a,b)=>a-b)
console.log(array,array2) //[2, 4, 5, 6, 7, 9] [2, 4, 5, 6, 7, 9]
//原array:[ 7, 2, 9, 4, 5, 6 ]

(3)自定义降序

var array = [ 7, 2, 9, 4, 5, 6 ]; 
let array2 = array.sort((a,b)=>b-a)
console.log(array,array2)  //[9, 7, 6, 5, 4, 2] [9, 7, 6, 5, 4, 2]
//原array:[ 7, 2, 9, 4, 5, 6 ]

11.indexOf():从头到尾找,返回数组中某个指定的元素所在位置的索引,没找到返回-1(找最开始出现的位置)

let array = ['❆LO❆','AND','❆VE❆','❆LOVE❆']
let index = array.indexOf('❆LO❆')
console.log(array,index) //['❆LO❆', 'AND', '❆VE❆', '❆LOVE❆'] 0

lastIndexOf():从尾到头找,返回数组中某个指定的元素所在位置的索引,没找到返回-1(找最后出现的位置)

let array = ['❆LO❆','AND','❆VE❆','❆LO❆','❆LOVE❆']
let index = array.lastIndexOf('❆LO❆')
console.log(array,index)  //['❆LO❆', 'AND', '❆VE❆', '❆LO❆', '❆LOVE❆'] 3

12.forEach():循环遍历数组中的每一项

let array = ['❆LO❆','AND','❆VE❆','❆LO❆','❆LOVE❆']
array.forEach((item,index)=>{
    console.log("值:"+item+",索引值为:"+index);
    /*
        值:❆LO❆,索引值为:0
        值:AND,索引值为:1
        值:❆VE❆,索引值为:2
        值:❆LO❆,索引值为:3
        值:❆LOVE❆,索引值为:4
    */
});
console.log(array) //['❆LO❆', 'AND', '❆VE❆', '❆LO❆', '❆LOVE❆']

13.filter():创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素

let array = [ 7, 2, 9, 4, 5, 6 ]; 
let array2 = array.filter((item,index)=>{
    console.log("值:"+item+",索引值为:"+index);
    return item > 4
});
console.log(array,array2) //[7, 2, 9, 4, 5, 6] [7, 9, 5, 6]

14.find():返回通过测试(函数内判断)的数组的第一个元素的值,如果没有符合条件的,返回undefined

let array = [ 7, 2, 9, 4, 5, 6 ]; 
let item = array.find((item,index)=>{
    return item > 4
});
console.log(array,item) //[7, 2, 9, 4, 5, 6] 7

15.reduce():将一个函数作为累加器,数组中的每个值(从左到右)开始加,最终得到累加和。

let array = [ 7, 2, 9, 4, 5, 6 ]; 
let sum = array.reduce((total,num)=>total+num);
console.log(array,sum) //[7, 2, 9, 4, 5, 6] 33

16.set()map()

了解Set和Map的使用_❆VE❆的博客-CSDN博客

1.2 总结方法是否改变原数组

改变原数组

push()、pop()、shift()、unshift()、

splice()、reverse()、sort()

不改变原数组

toString()、join()、slice()、concat()、

every()、some()、indexOf()、lastIndexOf()、

forEach()、filter()、find()、reduce()

第二章 、字符串常用方法

1.charAt() :可返回指定位置的字符,语法:string.charAt(index)

  •  当index大于字符串长度时,返回' '
  • 返回的字符,第一个字符索引为 0, 第二个字符索引为 1,以此类推

charCodeAt():返回指定位置的字符的 ASC码

  • 当index大于字符串长度时,返回NAN
let str = "HELLO WORLD";
let char = str.charAt(2)
let charCode = str.charCodeAt(2)
console.log(char,charCode)// L 76

2.toLowerCase():把字符串转换为小写

toUpperCase():把字符串转换为大写

let str = "Hello World";
let str1 = str.toLowerCase()
let str2 = str.toUpperCase()
console.log(str,str1,str2) //Hello World  hello world  HELLO WORLD

3.substr():在字符串中抽取从指定下标开始的指定长度的字符

let str = "Hello World";
let str1 = str.substr(1,3)
console.log(str,str1) // Hello World  ell
  • 参数:substr(start,length) 

        -- start:从哪个索引值开始

        -- length:需要抽取的字符串的长度

substring():提取字符串中介于两个指定下标之间的字符。与slice功能一样,但是区别是,参数不能是负数

let str = "Hello World";
let str1 = str.substring(1,4)
console.log(str,str1) // Hello World ell
  • 参数:string.substring(start,end) 

        -- start:从哪个索引值开始

        -- end:到哪结束(不包括end),当没有这个参数时,默认字符串长度

slice():提取字符串的某个部分,并以新的字符串返回被提取的部分。

let str = "Hello World";
let str1 = str.slice(1,4)
let str2 = str.slice(-5,-1)
console.log(str,str1,str2) //Hello World  ell  Worl

4.trim():去除字符串两边的空格

let str = "          Hello World                ";
let str1 = str.trim()
console.log(str,str1) //          Hello World                 Hello World

5.repeat():字符串复制指定次数,并直接连接一起

let str = "Hello World!";
let str1 = str.repeat(3)
console.log(str,str1) //Hello World!  Hello World!Hello World!Hello World!
  • 参数:string.repeat(count) 

        -- count:需要复制的次数

6.split():把一个字符串分割成字符串数组(根据自己指定的符号)

let str = "Hello World you!";
let str1 = str.split(' ') //根据空格分割字符串成数组
console.log(str,str1) //Hello World you!  ['Hello', 'World', 'you!']

7.replace():用另一个值替换在字符串中指定的值(默认只替换首个匹配 ),或替换一个与正则表达式匹配的子串。

let str="Mr Blue has a blue house and a blue car";
let str1 = str.replace("blue","red") //只替换首次出现的
console.log(str,str1) //blue house blue a blue car | red house blue a blue car
//--------------正则表达式-------------------
let str1=str.replace(/blue/g,"red");
console.log(str,str1) //Mr Blue has a blue house and a blue car | Mr Blue has a red house and a red car

8.indexOf():返回某个指定的字符串值在字符串中首次出现的位置

lastIndexOf():指定文本在字符串中最后一次出现的索引(位置)

let str = "you World you!";
let index1 = str.indexOf('you')
let index2 = str.lastIndexOf('you')
console.log(str,index1,index2) //you World you! 0 10

第三章、数值常用方法

1.toFixed():返回字符串,包含了指定位数小数的数字

let number = 8.959;
console.log(number.toFixed(0)) //9
console.log(number.toFixed(2)) //8.96
console.log(number.toFixed(4)) //8.9590
console.log(number.toFixed(6)) //8.959000

2.toPrecision():返回字符串值,它包含了指定长度的数字

let number = 8.959;      
console.log(number.toPrecision())  //8.959
console.log(number.toPrecision(2)) //9.0  
console.log(number.toPrecision(4)) //8.959
console.log(number.toPrecision(6)) //8.95900

3. Number():把 JavaScript 变量转换为数值

var x1 = true;
var x2 = false;
var x3 = new Date();
var x4 = "10"
var x5 = "10 20"
Number(x1);     // 返回 1
Number(x2);     // 返回 0
Number(x3);     // 返回 1673460165484
Number(x4);     // 返回 10
Number(x5);     // 返回 NaN

4.parseInt():解析一个字符串,并返回一个整数,返回首个数字,若不是数字,返回NAN

parseInt("10");         // 返回 10
parseInt("10.33");      // 返回 10
parseInt("10 20 30");   // 返回 10
parseInt("10 years");   // 返回 10
parseInt("years 10");   // 返回 NaN

5. parseFloat():解析一个字符串,并返回一个浮点数。只返回首个数字

parseFloat("10");        // 返回 10
parseFloat("10.33");     // 返回 10.33
parseFloat("10 20 30");  // 返回 10
parseFloat("10 years");  // 返回 10
parseFloat("years 10");  // 返回 NaN

第四章、Math常用方法

1.max():返回多个指定的数中最大值

console.log(Math.max(5,10,12,6,7,9)) // 12

min():返回多个指定的数中最小值

console.log(Math.min(5,10,12,6,7,9)) // 5

2.abs(x):返回x的绝对值

console.log(Math.abs(-7.25)) // 7.25

3.ceil(x):对x进行上舍入

console.log(Math.ceil(1.4))  // 2 

floor(x):对x进行下舍入

console.log(Math.floor(1.6)) // 1 

4.pow(x,y):返回 x 的 y 次幂

console.log(Math.pow(4,3)) // 64

sqrt(x):返回数的平方根

console.log(Math.sqrt(9)) // 3

5.round(x):对x四舍五入

console.log(Math.round(2.5)) // 3

6.trunc(x) :将x的小数部分去掉,只保留整数部分

console.log(Math.trunc(2.56)) // 2

第五章、Data常用方法

let date = new Date();
console.log(date.getDate());//日
console.log(date.getDay());//星期几
console.log(date.getMonth()+1);//月
console.log(date.getFullYear());//年份
console.log(date.getHours());//小时
console.log(date.getMinutes());//分钟
console.log(date.getSeconds());//秒
console.log(date.getMilliseconds());//毫秒
console.log(date.getTime());//时间数,1970年1月1日至今(data)的毫秒数

  • 12
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值