Array.prototype.includes(包含,返回数组内是否存在指定元素)
[1,2,3,4,5].includes(1) // ====》true
[1,2,3,4,5].includes(6) // ====》false
Array.from new Set(一行代码实现数组去重)
Array.from(new Set([1, 1, 1, 2, 3, 2, 4])) ===》 [1,2,3,4];
[...new Set([1, 1, 1, 2, 3, 2, 4])] ===》 [1,2,3,4];
Array.filter(过滤)
const arr1 = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4];
const arr2 = arr1.filter( (element, index, self) => {
return self.indexOf( element ) === index;
})
console.log(arr2); // => [1, 2, 3, 5, 4]
Array.find(查询数组中符合条件的对象,返回一个对象(有多个符合条件时,返回第一个))
const pets = [
{ type: 'Dog', name: 'Max'},
{ type: 'Cat', name: 'Karl'},
{ type: 'Dog', name: 'Tommy'},
]
pet = pets.find(pet => pet.type==='Dog' && pet.name === 'Tommy');
console.log(pet); // { type: 'Dog',name: 'Tommy' }
Object.assign()(可以处理一层的深度拷贝)
varobj1 = { a: 10, b: 20, c: 30 };
var obj2 =Object.assign({}, obj1);
obj2.b = 100;
console.log(obj1);
// { a: 10, b: 20, c: 30} <-- 沒被改到
console.log(obj2);
// { a: 10, b: 100, c:30 }