Array. The find ((item, indexArr, arr) = > {})

Find the first array member that matches the criteria. Its argument is a callback function that is executed on all the array members in turn. Until the first member whose return value is true is found, then the member is returned. If there is no qualified member, undefined is returned. Let arr = [10, 20, 30] let firstItem = arr. Find ((item, index, Arr) => { return item > 15 }) console.log('firstItem==>', firstItem); Let arr = [{age: 10}, {age: 20}, {age: 20}, {age: 20} 30 }] let firstItem = arr.find((item, index, Arr) => { return item.age > 19 }) console.log('firstItem==>', firstItem); // output {age: 20}Copy the code

Array.findindex ((item, index, Arr) => {}

The findIndex method on an array instance is used much like the find method, returning the location of the first qualified array member, or -1 if all members fail. let arr = [{ age: 10 }, { age: 20 }, { age: 30 }] let a = arr.findIndex((item, index, Arr) => { return item.age > 15 }) let b = arr.findIndex((item, index, Arr) => { return item.age > 45 }) console.log('a==>', a); // Output 1 console.log('b==>', b); Const arr = [{id: 001}, {id: 002}, {id: 003}]; let index = arr.findIndex(item => { return item.id == '004' }) console.log(index);Copy the code

Array.flat() for flattening nested arrays [Recommended – super handy]

The members of arrays are sometimes still arrays, and array.flat () is used to "flatline" nested arrays into one-dimensional arrays. This method returns a new array with no effect on the original data. [1, 2, [3, 4].flat ipA [fu lat] flat() only flattens one layer by default, if you want to flatten a nested array of multiple layers. The argument to the flat() method can be written as an integer representing the number of layers you want to flatten, which defaults to 1. [1, 2, 3, 4, 5]]]. Flat () in the code above, said want to leveling layer, the default value is 1 / / [1, 2, 3, [4, 5]] of [1, 2, [3, [4, 5]]]. Flat (2) in the code above, flat () parameters of 2, Represents a nested array to "flatten" two layers. [1, 2, 3, 4, 5] If you want to convert to a one-dimensional array regardless of the number of nesting levels, you can use the Infinity keyword as an argument. [1, [2, [3]].flat(Infinity) // [1, 2, 3] If the array is empty, flat() skips the empty. [1, 2, , 4, 5].flat() // [1, 2, 4, 5]Copy the code

Array.at() returns the value of the corresponding subscript

We all know that JavaScript does not support negative index values for arrays. So to represent the last member of the array, instead of writing arr[-1], you can only use arr[arr.length-1]. To solve the problem of negative indexing, ES6 added the at() method to array instances, which takes an integer as an argument. Returns the member at the corresponding position, with negative index support. This method works not only with arrays but also with arrays of strings and types (TypedArray). If the argument position is outside the range of the array, at() returns undefined. const arr = [100, 120, 18, 130, 4]; console.log(arr.at(1)) //120 console.log(arr.at(-1)) //4 console.log(arr.at(-5)) //100 console.log(arr.at(-6)) //undefinedCopy the code

Array. From ()

An array-like object, array. from turns it into a real Array. Note that this array-like object must have a length property in order to be converted to an array. Or will turn into an empty array let arrayLike = {' 0 ':' a ', '1', 'b', '2', 'c', length: 3}; Var arr1 = [].slice.call(arrayLike); / / / 'a', 'b', 'c'] / / written ES6 let arr2 = Array. The from (arrayLike); / / [' a ', 'b', 'c'] when there is no similar to an array of objects have no length attribute let arrayLike = {' 0 ':' a ', '1', 'b', '2', 'c'}; Let arr2 = array. from(arrayLike); / / []Copy the code

An Array of ()

The array.of () method converts a set of values to an Array. Const a = array. of(10, 20, 26, 38); console.log(a); // [10, 20, 26, 38] const b = Array.of(1).length; console.log(b); // 1 array.of () can be simulated with the following code: function ArrayOf() {return [].slice.call(arguments); }Copy the code

The use of Array. Includes

The array.prototype. includes method returns a Boolean value indicating whether an Array contains a given value. Similar to the includes method for strings. ES2016 introduced this approach. Const arr = [100, 200, 300]; console.log(arr.includes('100')) //false console.log(arr.includes(100)) //trueCopy the code
In the absence of this method, we use the indexOf method of the array to check if it contains a value. if (arr.indexOf(el) ! == -1) {// have this value} The indexOf method has two drawbacks. First, it is not semantic enough to find the first occurrence of a parameter value. Second, it uses the strict equality operator (===) internally for judgment, which leads to misjudgment of NaN. [NaN].indexof (NaN) // -1 includes does not contain this problem. [NaN].includes(NaN) // trueCopy the code

Extended operators (…)

The extension operator is three dots (...) Convert an array to a comma-separated sequence of arguments. console.log(... [1, 2, 3]) // 1 2 3 console.log(1, ... [2, 3, 4], 5) // 1 2 3 4 5 [...document.querySelectorAll('div')] // [<div>, <div>, <div>]Copy the code
Let arr1 = [11, 22,]; let arr2=["aa","bb"]; // let arr=arr1.concat(arr2); console.log(arr) // [11, 22, "aa", "bb"] //es6 let newarr=[...arr1,...arr2] console.log(newarr) // [11, 22, 33, 55, "aa", "bb", "cc", "dd"]Copy the code
// Arguments can get arguments from an object inside the function. Function sun(){console.log(arguments) // arguments (8) [1, 2, 3, 4, 5, 6, 7, 9, callee: ƒ, Symbol (Symbol. The iterator) : ƒ] he is a pseudo array} sun,2,3,4,5,6,7,9 (1); / / how to function within the pseudo Array into a true Array method 1 () function sun {let ags = Array. Prototype. Slice. The call (the arguments); ags.push(150); console.log(ags); / / [1, 2, 3, 4, 5, 6, 7, 9, 150]} sun,2,3,4,5,6,7,9 (1); Function sun(){let ags=[...arguments]; function sun(){let ags=[...arguments]; // Add pseudo-array ags.push(150); console.log(ags); / / [1, 2, 3, 4, 5, 6, 7, 9, 150]} sun,2,3,4,5,6,7,9 (1); // Summary extension operator is... [... objects that become real arrays]Copy the code

The vacancy of an array

A void in an array means that there is no value at any point in the array. For example, the Array() constructor returns arrays that are empty. let arr = new Array(3) console.log(arr); In the code above, Array(3) returns an Array with three empty Spaces.Copy the code
ES5's treatment of empty seats is already quite inconsistent, mostly ignoring empty seats. ForEach (), filter(), reduce(), every() and some() all skip empty Spaces. Map () skips empty space but keeps the value join() and toString() treat empty space as undefined, while undefined and null are treated as empty strings. Ps :ES6 explicitly converts empty space to undefined. let arr = new Array(3) console.log(arr[0] === undefined); //trueCopy the code
Ps :ES6 explicitly converts empty space to undefined. The array.from () method converts the empty space of the Array to undefined, meaning that it does not ignore the empty space. Array. The from ([' a ', 'b']) / / [" a ", undefined, "b"] extension operators (...). It also changes the empty space to undefined. [...['a',,'b']] // [ "a", undefined, "b" ] new Array(3).fill('a') // ["a","a","a"] for... The of loop also iterates over the empty space. let arr = [, ,]; for (let i of arr) { console.log(1); } // 1 // 1 in the above code, arr has two empty Spaces, for... Of doesn't ignore them. If the map() method is used instead, the empty space will be skippedCopy the code