ES6函数和数组补漏

对象的函数解构

let json = {
  a: 'sun',
  b: 'js'
}
function fun({a, b}) {
  console.log(a, b); //sun js
}
fun(json)
 
数组的解构
let arr=['js','sun','JS']
function fun(a,b,c){
  console.log(a,b,c); // js sun JS
}
fun(...arr)
 
in 的用法 ==> 判断是否存在,返回Boolean值
let obj = {
  a: 'sun',
  b: 'js'
}
console.log('a' in obj); //true
 
判断数组是否为空位,返回Boolean值
let arr = ['js',,,]
console.log(arr.length);
console.log(0 in arr); //true
console.log(1 in arr); //false
 
数组的遍历
(1)forEach方法 ==> 第一个参数为值,第二个参数为索引,还可以有第三个参数,是数组本身,一般情况下用不到
let arr=['js','sun','JS']
arr.forEach((val,index)=>{
  console.log(index,val);
})
(2)filter方法
let arr=['js','sun','JS']
arr.filter(x=>{
  console.log(x);
})

(3)some方法

let arr=['js','sun','JS']
arr.some(x=>{
  console.log(x);
})

(4)map方法 ==> 替换

let arr=['js','sun','JS']

console.log(arr.map(x=>'web'));

(5)数组转变为字符串

let arr=['js','sun','JS']

ES5方法:

console.log(arr.toString());

ES6方法:

console.log(arr.join('-'));

猜你喜欢

转载自www.cnblogs.com/sunyang-001/p/10850353.html