ES6(不常用且好用的)数组对象中新增的方法
isArray,isObject : 判断是不是一个数组 / 对象
数组方法
- map() : 返回一个新的数组,参数接收一个函数
let ary = [1, 2, 3, 4, 5, 6, 7];
let newAry = ary.map((value, index, arr) => value);
console.log(newAry);//[1, 2, 3, 4, 5, 6, 7]
- filter() : 过滤,返回为true的值
let arr=[1,2,3,4,3,2,1,2];
let newArr1=arr.filter((value,index,arr)=>value>=3)
console.log(newArr1);//输出[3,4,3]
- reduce : 第一个参数也是函数,接受四个参数(上一次return的值,当前项的值,当前位置,原数组),刚开始遍历,默认前一项值是第一项值,当前项是第二项值,reduce还接受第二个参数,给前一项值设置初始值,此时的函数中的当前值就是从第一项开始
- reduceRight : reduce是从左到右遍历,reduceRight是从右向左遍历
//以前找最大值,最小值我们是用的2层循环来找的,现在直接一行代码搞定,是不是很爽
let arr=[1,2,3,4,3,2,1,2];
let newArr2=arr.reduce((pre,cur,curIndex,arr)=>pre>cur?pre:cur)
//pre=1不大于cur=2,返回2;
//pre接收返回值2;pre=2不大于cur=3,返回3
.....
//一直找到4并且返回4;
//pre接收4,pre=4大于pre=3然后就一直返回的都是4,这样就会找到最大值
console.log(newArr2)//输出4,找到最大值,如果想找最小值只需要pre<cur?pre:cur
Array.from() : 把一个类似数组或可以迭代的对象中创建一个新的数组实例,它接收三个参数,第一个是想要转换的数组或者对象,第二个类似于数组的map方法,对每个元素进行处理再返回,第三个参数是改变第二个参数中的this
let list = document.querySelectorAll("*");
console.log(Array.from(list));//[html, head, meta, title, body, script]
let set = new Set([1, 2, 3, 4, 5, 6, 7, 8]);
console.log(Array.from(set));//[1, 2, 3, 4, 5, 6, 7, 8]
//他们的__proto__都指向Array的原型
Array.of() : 将一组值转换为数组,解决了构造函数方式创建数组传一个参数异常的问题
console.log(Array.of(1, 2, 3, 4, 6));// [1, 2, 3, 4, 6]
console.log(Array.of(1, 2, 3, 4, 6));// [1, 2, 3, 4, 6]
console.log(new Array(6));//[empty × 6]
console.log(Array.of(6));//[6]
copyWidthin() : copyWidthin方法可以在当前数组内部,将指定位置的数组项复制到其他位置(会覆盖原数组项),然后返回当前数组。使用copyWidthin方法会修改当前数组。
Array.prototype.copyWithin(target, start = 0, end = this.length)
target: 这个参数是必须的,从该位置开始替换数组项
start: 这是一个可选参数,从该位置开始读取数组项,默认为0,如果为负值,表示从数组的右边向左开始读取
end: 这是一个可选参数,到该位置停止读取的数组项,默认等于Array.length。如果为负值,表示倒数
-----------
items.copyWithin(6, 1, 3)
// <- [1, 2, 3, undefined × 3, 2, 3, undefined × 2]
- Array.prototype.fill方法使用给定的值填充一个数组:用于空数组的初始化很方便,数组中已经有的元素会全部抹去,Array.prototype.fill方法还可以接受第二个和第三个参数,用于指定填充的起始位置和结束位置。
let ary = [1, 2, 3, 4, 5, 6, , , , ,];
ary.fill(3,6,8);//(10) [1, 2, 3, 4, 5, 6, 3, 3, empty × 2]
console.log(ary);
- find() : 用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的数组项,然后返回该数组项。如果没有符合条件的数组项,则返回undefined。
[1, 2, 3, 4, 5].find(item => item > 2)//3
[1, 2, 3, 4, 5].find((item, i) => i === 3)//4
[1, 2, 3,5].find(item => item === Infinity)//undefined
- findIndex() : 用来返回数组项在数组中的位置。其和Array.prototype.find方法非常类似,接受一个回调函数,如果符合回调函数的条件,则返回数组项在数组中的位置,如果所有数组项都不符合回调函数条件,则会返回-1。
[1, 2, 3, 4, 5].findIndex(item => item > 2)//2
[1, 2, 3, 4, 5].findIndex((item, i) => i === 3)//3
[1, 2, 3, 4, 5].findIndex(item => item === Infinity)//-1
ES6中的遍历方法
entries()、keys()和values(),用来遍历数组。它们都返回一个遍历器对象,可以用for…of循环进行遍历,唯一的区别是keys()是对数组的键名的遍历、values()是对数组键值的遍历,entries()方法是对数值的键值对的遍历。
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
如果不使用for…of循环,可以手动调用遍历器对象的next方法,进行遍历
let letter = ['a', 'b', 'c']; let entries = letter.entries(); console.log(entries.next().value); // [0, 'a'] console.log(entries.next().value); // [1, 'b'] console.log(entries.next().value); // [2, 'c']
对象方法
- Object.is(a,b) : 将a和b进行比较,解决了NaN在===比较的时候不相等的问题
还有+0和-0 三个等于号比较的时候不相等的问题
console.log(NaN === NaN); //false
console.log(Object.is(NaN, NaN)); //true
console.log(Object.is(+0, -0)); //true
console.log(+0 === -0); //fasle
- Object.assign(target, source…)方法的第一个参数是目标对象,后面的参数都是源对象。 Object.assign()主要用来拷贝对象。
Object.assign()主要用来拷贝对象。例如:
let s1 = {};
Object.defineProperty(s1, 'name', {
value: 'xiaoming',
configurable: true,
enumerable: true,
writable: true
});
Object.defineProperty(s1, 'age', {
value: 21,
configurable: true,
enumerable: false, //这个属性为不可枚举
writable: true
});
let s2 = Object.assign({},s1);
console.log(s2); // Object {name: "xiaoming"}
这里的Object.assign()拷贝的性质还是属于浅拷贝,如果你需要深拷贝的话:
let s1 = {
name: 'xiaoming',
age: 21,
info: {
childhood: {
name: 'xiao'
},
youth: {
name: 'xiaoming'
}
}
}
let s2 = JSON.parse(JSON.stringify(s1));
console.log(s1 === s2); //false
console.log(s1.info === s2.info); //false
- Object.setPrototypeOf(obj, prototype)
- obj : 要设置其原型的对象
- prototype : 该对象的新的原型(一个对象或者null,如果是其他值,则什么都不会做)
首先要介绍一下,__proto__是一个内部属性,用来操作prototype。
直接使用prototype不是特别好,于是出了几个代替的方法:
1、Object.setPrototypeOf() 写操作(ES6)
2、Object.getPrototypeOf() 读操作(ES5)
3、Object.create() 生成操作(ES5)
---------------------------
Object.getPrototypeOf
---------------------------
function People(){};
let p = new People();
console.log(Object.getPrototypeOf(p) === People.prototype); // true
--------------------
Object.create()
--------------------
let o = {};
//上面的操作等价于 let o = Object.create(Object.prototype);
-----------------------------
Object.setPrototypeOf()
-----------------------------
let fa = {};
let ch = {
x: 10
}
Object.setPrototypeOf(ch, fa);
fa.y = 20;
console.log(ch.y); //20