1、Array.prototype.flat() 数组扁平化
const arr = [1,[2,3],[4,[5,[6]],7]];
// 不传参数时,默认“拉平”一层
arr.flat()
// [1,2,3,4,[5,[6]],7];
// 传入一个整数参数,整数即“拉平”的层数
arr.flat(2)
// [1,2,3,4,5,[6],7];
// Infinity 关键字作为参数时,无论多少层嵌套,都会转为一维数组
arr.flat(Infinity);
// [1,2,3,4,5,6,7];
// 传入 <=0 的整数将返回原数组,不“拉平”
arr.flat(0);
// [1,[2,3],[4,[5,[6]],7]]
arr.flat(-6);
// [1,[2,3],[4,[5,[6]],7]]
// 如果原数组有空位,flat()方法会跳过空位
[1,2,3,4,5,6,,].flat();
// [1,2,3,4,5,6]
2、Object.keys 遍历对象
let person = {name:{aaa: "小王"},age:{bbb:222},address:{ccc:"深圳"}}
Object.keys(person).map((key)=>{
person[key] // 获取到属性对应的值,做一些处理
})
//返回 {aaa: "小王"} {bbb:222} {ccc:"深圳"}
3、includes() 数组是否包含
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(3)); //返回true
console.log(numbers.includes(6)); //返回false