1. 扩展运算符(...)
let arr = [1,2,3,4,5,6]
console.log(...arr); //1,2,3,4,5,6
//用于数组的合并
let arr1 = [1,2];
let arr2 = [3,4];
let arr3 = [...arr1,...arr2];
//用于解构赋值
const [first,...rest] = [1,2,3,4,5];
console.log(first) // 1
console.log(rest) //[2,3,4,5]
//如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错。
const [...butLast, last] = [1, 2, 3, 4, 5];
// 报错
const [first, ...middle, last] = [1, 2, 3, 4, 5];
// 报错
2. 对象的扩展运算符
//对象的扩展运算符(...)用于取出参数对象的所有可遍历属性,拷贝到当前对象之中。
let z = { a: 3, b: 4 };
let n = { ...z };
n // { a: 3, b: 4 }
//扩展运算符可以用于合并两个对象。
let ab = { ...a, ...b };
// 等同于
let ab = Object.assign({}, a, b);
let aWithDefaults = { x: 1, y: 2, ...a };
// 等同于
let aWithDefaults = Object.assign({}, { x: 1, y: 2 }, a);
// 等同于
let aWithDefaults = Object.assign({ x: 1, y: 2 }, a);
3. 解构赋值
对象的解构赋值用于从一个对象取值,相当于将目标对象自身的所有可遍历的(enumerable)、但尚未被读取的属性,分配到指定的对象上面。所有的键和它们的值,都会拷贝到新对象上面。
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
x // 1
y // 2
z // { a: 3, b: 4 }
由于解构赋值要求等号右边是一个对象,所以如果等号右边是undefined或null,就会报错,因为它们无法转为对象。
let { ...z } = null; // 运行时错误
let { ...z } = undefined; // 运行时错误
解构赋值必须是最后一个参数,否则会报错。
let { ...x, y, z } = someObject; // 句法错误
let { x, ...y, ...z } = someObject; // 句法错误
4. 数组的新方法
1.foreach()
var arr = [1,2,3,4];
arr.foreach((item,index,arr)=>{
console.log(item); //结果为1,2,3,4
});
//foreach遍历数组,无返回值,不改变原数组,仅仅只是遍历,常用于注册组件、指令等等。
2.map()
var arr = [1,2,3,4];
arr.map((item,index,arr)=>{
return item*10; //结果为10,20,30,40
});
//map遍历数组,返回一个新数组,不改变原数组
3.filter()
var arr = [1,2,3,4];
arr.filter((item,index,arr)=>{
return item >2; //结果为[3,4]
});
//filter过滤掉数组中不满足条件的值,返回一个新数组,不改变原数组
4.reduce()
var arr = [1,2,3,4];
arr.reduce((result,item,index,arr)=>{
console.log(result);
console.log(item);
console.log(inddx);
return result+item;
});
//reduce让数组的前后两项进行某种计算,然后返回其值,并继续计算,不改变原数组,返回计算的最终结果,从数组的第二项开始遍历。
5.some()
var arr = [1,2,3,4];
arr.some((item,index,arr)=>{
return item > 2; //返回true
});
//遍历数组每一项,有一项返回true,则停止遍历,结果返回true。不改变原数组。
6.every()
var arr = [1,2,3,4];
arr.every((item,index,arr)=>{
return item >1; //返回false
});
//遍历数组每一项,每一项返回true,最终结果为true.有一项返回false,停止遍历,结果返回为false。不改变原数组。
数组实例的 find() 和 findIndex()
find()
方法用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员。如果没有符合条件的成员,则返回undefined。
var aaa = [1, 2, 3, 4, 5].find(function(value, index, array) {
return value > 2
});
console.log(aaa); //3
findIndex()
方法的用法与find()
方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1。
var aaa = [1, 2, 3, 4, 5].findIndex(function(value, index, array) {
return value > 2
});
console.log(aaa); //2
这两个方法都可以接受第二个参数,用来绑定回调函数的this对象。
function getAge(age) {
return age > this.age;
}
let person = {
name: "ciel",
age: 10
};
console.log([10, 20, 30].find(getAge, person)); //20
数组实例的 entries(),keys() 和 values()
entries()
,keys()
和values()
用于遍历数组。它们都返回一个遍历器对象,可以用for...of
循环进行遍历,唯一的区别是keys()
是对键名的遍历、values()
是对键值的遍历,entries()
是对键值对的遍历。
for(let index of ["a", "b"].keys()) {
console.log(index);
}
//0
//1
for(let item of ["a", "b"].values()) {
console.log(item);
}
//a
//b
for(let [index, item] of ["a", "b"].entries()) {
console.log(index, item);
}
//0 "a"
//1 "b"