【Es6入门实践】06数字的操作,数组及其相关方法的操作(重点)
程序员文章站
2022-04-25 09:53:01
...
01数字的操作
//二进制的声明
let binary=OB010101;
console.log(binary)
//八进制的声明
let octal=Oo666
console.log(octal)
let octal2=Oo777
console.log(octal2)
用Number.isFinite
来进行是字符串还是数字的判断
let a=11;
console.log(Number.isFinite(a))
console.log(Number.isFinite("hls"))
console.log(Number.isFinite(NaN))
console.log(Number.isFinite(undefined))
判断NaN
console.log(Number.isNaN(NaN))//true
console.log(Number.isNaN(1))//false
用Number.isInteger判断是否是整数
let a=110;
console.log(Number.isInteger(a))
用Number.parseFloat判断是否是浮点数
let b=110.1;
console.log(Number.parseFloat(b))
也可以使用!来判断是否是浮点数
let c=110;
let d=110.09;
console.log(!Number.isInteger(c))
console.log(!Number.isInteger(d))
Integer是有取值范围的
let hls=Math.pow(2,53)-1
console.log(hls)
最大安全数和最小安全数
console.log(Number.MAX_SAFE_INTEGER)
console.log(Number.MIN_SAFE_INTEGER)
console.log(Number.isSafeInteger(hls))
02数组的操作
- json数组格式
Array.from把数组格式转换成数组
let json = {
"0":"汉堡包",
"1":"炸鸡腿",
"2":"冰淇淋",
length:3
}
let arr = Array.from(json)
console.log(arr)
- 用Array.of转换成数组
let arr2 = Array.of(3,4,5,6)
console.log(arr2)
let arr3 = Array.of("健康","长寿","幸福","益友")
console.log(arr3)
- find()实例方法,先声明数组,之后find
value:表示当前查找的值
index:值的索引
arr:原型
let arr=[1,2,3,4,5,6,7,8]
console.log(arr.find(function(value,index,arr){
return value >7
}))
浏览器显示8,说明找到了大于7的值就返回,这就是es6提供的查找数组的方法
当然了,它也可以查找字符串
let arr=["牛奶","可乐","绿茶","冰红茶"]
console.log(arr.find(function(value,index,arr){
return value == "可乐"
}))
如果查找成功就返回这个数,如果查找不成功就返回undefined
- fill:填充
let arr=["牛奶","可乐","绿茶","冰红茶"]
console.log(arr.fill("咖啡",1))
如果说不想全部替换掉,可以使用第三个参数来决定替换的个数
let arr=["牛奶","可乐","绿茶","冰红茶"]
console.log(arr.fill("咖啡",1,3))
数组循环(重点)for of
let arr=["牛奶","可乐","绿茶","冰红茶"]
for(let item of arr){
console.log(item)
}
- 实例方法keys()
let arr=["牛奶","可乐","绿茶","冰红茶"]
for(let item of arr.keys()){
console.log(item)
}
- entries():下标和值一起输出
let arr=["牛奶","可乐","绿茶","冰红茶"]
for(let item of arr.entries()){
console.log(item)
}
还可以这样写
let arr=["牛奶","可乐","绿茶","冰红茶"]
for(let [index, val] of arr.entries()){
console.log(index+':'+val)
}
- entries()是实例方法,它还有另一个妙用:手动循环(特殊的循环方式)
let arr=["牛奶","可乐","绿茶","冰红茶"]
let list = arr.entries()
console.log(list)
console.log(list.next().value)
console.log("--------------")
console.log(list.next().value)
console.log("~~~~~~~~~~~~~~")
console.log(list.next().value)
console.log("^^^^^^^^^^^^^^")
数组是很重要的一个知识点,要掌握它,就必须多动手实践