An array of 1.

1.Array.of() // Create an Array with the values of the parameters as elements
Let STR ='1,2 'array. of(STR) // [1,2,3]Copy the code
2.Array.from() // Convert an array-like object or traversable object to a true Array
eg: A. Class array is converted to a real array (must have length property and the property name is number, Type or a string of Numbers) let obj = {0: 'Tom', / / '0', 'Tom' (fx) 'a' : 'Tom' (x) is undefined 1: '65', 2: 'male', 3: [' Jane ', 'John', 'Mary']. []} array. from(obj) // [" Tom ", "65", "male ", Array(3)] note: 1 If there is no length attribute, it returns an empty array [] 2. If the object's attribute name is not a numeric type, it returns the corresponding length of length of [,, undefined undefined undefined undefined] array b. Let arr3 = new Set([4,5,6]) array. from(arr3) // [4,5,6] c Let arr5 = array. from(arr4,item=>item*2) // [8,10,12] d: convert string to Array: Let str2 = 'hello' Array. The from (str2) / / [" h ", "e", "l", "l", "o"] e: parameter is an Array: Let arr7 = [1,2,3] let arr8 = [1,2,3] array. from(arr7) // [1,2,3] array. from(arr8) // Let map = new map () map.set('name',' red ') map.set(true,' male ') The console. The log (map) / / {" name "= >" little red ", true = > "male"} Array. The from (map) / / [[" name ", "little red"], [true, "male"]]Copy the code
4.Array.filter() // Array filtering
Let a = ({name: "kele", the title: "Coca-Cola"}, {name: 'kele, title:' the fender '}, {name: 'WLG, title: }] let b = a.filter(item => item.name === 'kele'); The console. The log (b) / / [{name: 'kele, title:' Coca-Cola '}, {name: 'kele, title:' the fender}]Copy the code
5.Array.every() // returns true when all elements in the Array meet the criteria
Let a = [1, 2, 3, 4, 5]; let b = a.every(item => item > 2); console.log(b) // falseCopy the code
6.Array.some() // Return true if there is an element in the Array that meets the criteria
Let a = [1, 2, 3, 4, 5]; let b = a.some(item => item > 2); console.log(b) // trueCopy the code
7.array.find () // Returns the first element in the Array that meets the criteria, otherwise undefined
Let a = [1,2,3,4,5] let b = a.type (item => item > 2) console.log(b) // 3Copy the code
8. Array.findindex () // Returns the index of the first eligible element in the Array, otherwise -1
Let a = [1,2,3,4,5] let b = a.indindex (item => item > 2) console.log(b) // 2Copy the code
Array.includes(val,index) // If the Array contains the specified value, return true, otherwise false equals indexof, the first parameter is mandatory, the search value, the second parameter is the start of the search position
Let a = [1,2,3,4,5] let b = a.includes(2) console.log(b) // trueCopy the code
Array.map(callback) // Returns a new Array based on the conditions in your callback function
Log (b) // [2,4,6,8,10] returns a brand new arrayCopy the code
Array.reduce(callback) // Perform class-add operations on each element in the Array according to the conditions in callback, returning a new value
Let a = [1,2,3,4,5] let b = a. duce((pre,cur)=>{return pre+cur}) console.log(b) // 15 sum operationCopy the code
11. Merge arrays – operator extensions
Let a = [1, 2, 3] let b = (4 and 6) let c = [... a, b]...Copy the code
12. Add array extension:
Arr. Flat (depth) // depth: Let a = [1,2,3,5, [678,[4,8,9]]]] let b = a.flat(3) // [1,2,3,5,678,4,8,9] Method 2: using the reduce function iteration var arr = [1, [2, [3, 4, 5), 6]]]. function flatten (arr) { return arr.reduce((pre, item) => Array.isArray(item) ? pre.concat(flatten(item)) : pre.concat(item) , []) } console.log(flatten(arr)); //[1, 2, 3, 4, 5, 6] Function flatten(arr) {while (arr.some((item) => array.isarray (item))) {arr = [].concat(... Arr)} return arr} let arr = [1, 2, [3, 4], [5, [6, 7]] console.log(flatten(arr)) b: for,forEach, for.. ForEach can't jump out of the loop. ForEach can't jump out of the loop. The for and the for... Of can be skipped or interrupted using break or continue. for ... Of accesses the actual element directly. The forEach callback function has more arguments for iterating through a set of indexes, including elements, indexes, and arrays. for ... Of and for are also executed if there are empty elements in the array. 2. Some, some, and some can be broken if an element is true. Return false if all elements do not meet the condition. Every, as opposed to some, returns false if the beneficial element does not satisfy the condition, and the loop is broken. Return true for all elements. 3. Difference between filter and map: both generate a new array and do not change the original array (except for iterating through the array of objects, operating on element objects in the callback function). Both skip empty elements and map will form a new array with the same length as the original array. Filter forms a new array of elements that meet the criteria of the callback function, with a different length. The new array elements generated by map are customizable. The new array element generated by filter cannot be customized and is the same as the original array element. 4. The difference between find and findIndex is that both are used to find elements in an array. The find method returns the value of the first element in the array that satisfies the callback function. Return undefined if none exists. FindIndex returns the index of the element found in the array, not its value, or -1 if none exists. 5. Reduce and reduceRight differences: reduce method receives two parameters, the first parameter is callback and the second parameter is initialValue. ReduceRight method is completely consistent with reduce except that it is executed in the opposite direction (from right to left). The callback function receives four parameters: accumulator: MDN is interpreted as an accumulator, but I feel it is not appropriate, according to my understanding it should be up to the current element, all the elements before the array was processed by the callback function accumulated result. Current: the array element currently being executed. CurrentIndex: Index of the array element currently being executed. SourceArray: The original array, that is, the array from which the reduce method is called. If no initial value is passed in, the reduce method executes the callback function starting at index 1, and if an initial value is passed in, the callback starts at index 0 and accumulates from the initial value. Const list = [{name: 'left', width: 20}, {name: 'right', width: 10}, {name: 'center', width: 70}, {name: 'center', width: 70}, 'right', width: 10 }, { name: 'left', width: 20 }, { name: 'right', width: 10 }, ]; Const total = list.reduce((currentTotal, item) => {return currentTotal + item.width; }, 0); Const repeatTime = {}; // total: 140 const result = list.reduce((array, item) => { if (repeatTime[item.name]) { repeatTime[item.name]++; return array; } repeatTime[item.name] = 1; return [...array, item]; } []); // repeatTime: { left: 2, right: 3, center: 1 } // result: [ // { name: 'left', width: 20 }, // { name: 'right', width: 10 }, // { name: 'center', width: 70 }, // ] 3. Const Max = list.reduce((curItem, item) => {return curitem.width >= item.width? curItem : item; }); const min = list.reduce((curItem, item) => { return curItem.width <= item.width ? curItem : item; // max: { name: "center", width: 70 } // min: { name: "left", width: 20} 4. Reduce is very strong, more exotic curiosity-a solution looking recommend to view this article [the array of 25 you have to know the reduce advanced usage "] (https://juejin.cn/post/6844904063729926152)Copy the code

2. The object