JS方法大全

JS扩展

字符串

let str1=''
let str2=""
let str3=`反引号是Es6里的新语法`


console.log(Math.random()*255)
写一个函数,可以生成随机的颜色
function randColor() {
  return  "rgb("+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+")"
}
list.style.background=randColor()
let str4="I am a student!"

charAt() 返回在指定位置的字符,空格也占位置,几乎不用,因为如下可用str4[3]代替
console.log(str4.charAt(3))
console.log(str4[3])

concat链接两个字符串,生成新字符串
console.log(str4.concat(str3))//一般不用,用+更好
let num=123
console.log(num+"") //把数字123快速转换为字符串123,当+左右有一侧是字符串时,生成的也为字符串。

endsWith() 判断当前字符串是否以指定字符串结尾,区分大小写,返回布尔值
console.log(str4.endsWith("!"))

startWith() 判断当前字符串是否以指定字符串开头,区分大小写,返回布尔值
console.log(str4.startsWith("I"))

indexOf 返回某字符串在当前字符串中首次出现的位置,如果没有则返回 -1
console.log(str4.indexOf("a"))

lastIndexOf() 使用方法与上相同,返回某字符串在当前字符串中最后出现的位置
console.log(str4.lastIndexOf("a"))

includes 查找字符串中是否存在指定的字符串
console.log(str4.includes("a"))

match 查找到一个或多个正则表达式的匹配
console.log(str4.match(/a/g))//不加g,查找到第一个结果就结束并输出;加了g,找到一个不结束,继续找,知道吧所有满足条件的字符串都找出来为止
如果找不到则输出null

repeat 复制字符串指定的次数并连接到一起返回
console.log(str4.repeat(3))

replace 在字符串中查找对应的子串,然后替换成新内容,可以用正则,正则后面可以加g,则所有满足条件的字符串都将被替换
console.log(str4.replace("a","b"))

replaceAll 所有符合的字符串都替换,不能用正则
console.log(str4.replaceAll("a","b"))

let str5="abcdeFG1234567hijklmn"

search 查找字符串中满足正则的字符串,返回索引
console.log(str5.search(/\d{7}/))

slice 从字符串中截取某部分,含前不含后
console.log(str5.slice(7,14))

split 以某字符为界限切割字符串,生成新数组
console.log(str5.split(""))//以空为界限切割字符串

substring 从字符串中提取指定两个索引间的字符串,含前不含后
console.log(str5.substring(7,14))

substr 从字符串中提取指定位置指定数量的字符串
console.log(str5.substr(7,14))

toLowerCase 生成新字符串,把原字符串所有字母改成小写
console.log(str5.toLowerCase())

toUpperCase 生成新字符串,把原字符串所有字母改成大写
console.log(str5.toUpperCase())

let str6=" abc def "

trim 去掉字符串两边的空格,中间的不去
console.log(str6.trim())

toString 转换为字符串
console.log(str6.toString())



数组

数组方法 unshift,shift,push,pop,splice

concat 链接两个或更多数组,并返回结果
copyWithin 从数组的指定位置拷贝元素,到另一个指定位置去
every 判断数组中是否所有元素都满足回调函数中的条件,返回布尔值
let arr=[2,4,6,9,10]
console.log(arr.every(item=>item%2===0))
let years=[2000,2004,2008]
console.log(years.every(item=>item%4===0&&item%100!==0||item%400===0))

filter 过滤,把原数组中所有满足此条件的元素组成新数组
let arr2=[1,2,3,4,5,6,7,8,9]
let arr3=arr2.filter(item=>item%2===1)
console.log(arr3)

map 映射
let arr4=arr3.map(item=>item*2)
console.log(arr4)

reduce
reduceRight() 从右往左算
let total=arr4.reduce((pre,next)=>pre+next,0)
console.log(total)

console.log(
    arr2.filter(item=>item%2===1)
        .map(item=>item*2)
        .reduce((pre,next)=>pre+next,0)
)

typeof 判断数据类型,注意:判断生成的是个字符串,并且判断数组和对象时,生成的结果都是对象object,不准确
let arr7=["a","b","c"]
console.log(arr7.every(item=>typeof item))

find 如果有,则输出,如果没有,则输出undefined
console.log(arr.find(item=>item===2))
const arr8=[
    {account:123456,password:"abcdefg"},
    {account:123457,password:"abcdefg"},
    {account:123458,password:"abcdefg"},
    {account:123459,password:"abcdefg"}
]
console.log(arr8.find(item=>item.account===123458))

findIndex 查找对应元素的索引
console.log(arr8.findIndex(item=>item.account===123458))

froEach 跟map几乎一直,但是没有返回值,不能链式调用
arr2.forEach((item,index)=>arr2[index]=item*2)
console.log(arr2)

from
arr2=Array.from(arr2,item=>item/2)
console.log(arr2)

includes 判断数组中是否包含指定的值

indexOf 搜索数组中的元素,并返回它所在位置

lastIndexOf 返回它最后出现的位置

isArray 判断是否是一个数组
console.log(Array.isArray(arr2))
判断是否是个对象
1.
if (typeof arr8[0]==="object"&&!Array.isArray(arr8[0])){
    console.log("是对象")
}
2.数组有长度,对象没有长度
console.log(typeof arr8[0]==="object"&&!arr8[0].length)

join 数组转换为字符串,括号里默认以逗号分隔,也可以用toString
console.log(arr2.join())

reverse 反转数组
console.log(arr2.reverse())

反转字符串
let strA="a,b,c,d,e,f,g"
console.log(strA.split(",").reverse().join())

let strB="abcdefg"
console.log(strB.split("").reverse().join(""))

map 映射

pop push shift unshift splice

slice 选取数组中的一部分,返回新数组

some 检测数组中是否有元素符合指定条件,跟includes相同

sort 对数组元素排序
let arr9=["Banana","Orange","Apple","Mango"]
arr9.sort()//默认按照首字母升序排列
console.log(arr9)

let arr10=[8,7,45,3,9,97]
arr10.sort()//默认按照首字母升序排列
console.log(arr10)

按照数字升序排列
arr10.sort((next,pre)=>next-pre)
console.log(arr10)
按照数组降序排列
arr10.sort((next,pre)=>pre-next)
console.log(arr10)

数组乱序
let arr11=["a","b","c","d","e","f","g","h","i","j","k"]
console.log(arr11.sort(()=>Math.random()-.5))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值