// es6引入rest参数,用于获取函数的实参,用来代替arguments
// es6获取实参的方式
function date(){
console.log(arguments);
}
date('a','b','c')
//rest实参
function date(...args){
console.log(args);
}
date('a','b','c')
//rest参数必须要放到参数最后
function fn(a,b,...args){
console.log(a);
console.log(b);
console.log(args);
}
fn(1,2,3,4,5,6)
// [...]扩展运算能将[数组]转换为逗号分隔的[参数序列]
const arr=['a','b','c','d']
function chuanwan(){
console.log(arguments);
}
chuanwan(...arr);
// 数组的合并
const yidui=['a',"b","c"]
const erdui=['d','e','f']
// const hedui=yidui.concat(erdui);
const hedui=[...yidui, ...erdui]
console.log(hedui);
// 数组的克隆
const kaishi=['E','G','M']
const clone=[...kaishi]
console.log(clone);
// 将伪数组转化为真正的数组
const divs=document.querySelectorAll("div")
const divArr=[...divs];
console.log(divArr); //arguments
// 构造函数方法:Array.from()
// 将类数组或课遍历数组对象转换为真正的数组
let arrayLike={
'0':'a',
"1":"b",
"2":"c",
length:3
};
// let arr2=Array.from(arrayLike); //['a','b','c']
// 方法还可以接受第二个参数,作用类似于数组的map方法,用来堆每个元素进行处理
// 将处理后的值放回返回的数组
var arryLike={
"0":"1",
"1":"2",
"length":2
}
Array.from(arrayLike,item=>{
return item*2
})
console.log(ary);//2,4
// Arry 扩展方法
// find()
// 用于找出第一个符合条件的数组成员,如果没有找到返回undefined
let ary=[{
id:1,
name:'张三'
},{
id:2,
name:'李四'
}];
let target=ary.find((item,index)=>item.id==2)
// Array的扩展方法
// findIndex()用于找出弟也给符合条件成员的位置,如果没有找到返回-1
let ary=[1,5,10,15];
let index=ary.findIndex((value,index)=>value>9);
console.log(index); //2
// includes()
// 显示某个数组是否包含给定的值,返回布尔值
[1,2,3].includes(2) //true
[1,2,3].includes(4) //false