进阶es6实战
1,数组去重:
const arr = this.ruleForm.authority.reduce(
(all, next) =>
all.some((atom) => atom["title"] == next["title"])
? all
: [...all, next],
[]
);
// this.ruleForm.authority为数组的名字
//:title匹配相同字段的名字
2,从数组中删除元素:
const arr = []
arr.splice(
this.ruleForm.authority.findIndex(
(item) => item.title === data.title
),
1
);
// this.ruleForm.authority为数组的名字
//:title匹配相同字段的名字
3,函数的扩展
1,函数参数的默认值
//直接将默认值写在形参之后,有默认值的形参要放在没有默认值形参之后
//语法:
function fun(x,y='word'){
console.log(x,y)
}
fun('hello','nihao');
fun('hello')
2、函数参数与解构结合使用
function fun({x,y=5=}={}){
console.log(x,y);
}
//注意:该函数如果传入对象,那么就与传入的对象进行解构,如果不传任何内容,就与空对象进行解构
3、函数参数默认值的位置
//如果函数有默认值,这个默认值参数一般会是尾参数(放在最后),如果非尾参数设置了默认值,那么这个参数必须要设置传值,默认值将没有意义
function fun(a,b=3,c){}
fun(1)
4、函数参数的个数
//形参个数:函数名.length
//函数形参个数不包括有默认值的参数以及默认值参数以后的所有参数,也不包括rest参数(...rest);
//实参个数:函数内部调用 arguments.length;
5、rest参数
es6 引入的rest参数(…rest),主要用于接收形参可接受参数之外的多余的实参,这样我们就不用使用arguments这个类数组了
rest 参数是一个数组,多余的实参都会存放进去
注意:如果一个函数有多个参数,无默认值的参数放在最前边,有默认值的参数放在中间,rest参数一定是最后边
数组解构与rest结合:
…[1,2,3] 将该数组解构(拆开) 1,2,3
- 个人项目实战经验,大佬勿喷!