ES6 - mowen0303/note GitHub Wiki
Use case
Array
1. array.includes()
determines whether an array includes a certain value among its entries, returning true or false as appropriate.
const pets = ['cat', 'dog', 'bat'];
array1.includes('cat') // expected output: true
2. array.every()
method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
const array1 = [1, 30, 39, 29, 10, 13];
const array2 = [1, 10];
array2.every(v=>array1.includes(v)) //expected output: true
3. array.some()
tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0; // checks whether an element is even
array.some(even) // expected output: true
4.array.join()
creates and returns a new string by concatenating all of the elements in an array
const elements = ['Fire', 'Air', 'Water'];
elements.join(); // expected output: "Fire,Air,Water"
elements.join(''); // expected output: "FireAirWater"
elements.join('-'); // expected output: "Fire-Air-Water"
5.array.concat()
合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2); // expected output: Array ["a", "b", "c", "d", "e", "f"]
loop
1. for...in
遍历对象,获取对象的键名
var obj = {a:1, b:2, c:3};
for (let key in obj) {
console.log(key);
}
//output
//a
//b
//c
2. for...of
遍历数组,直接获取值
var arr = [1,2,3];
for (let val in arr) {
console.log(val);
}
//output
//1
//2
//3