<> establish
var a = [1,2,3]
var a = Array()
<> increase
push(): Add an element at the end
unshift(): Add an element to the header
ES6: fill() Fill array
parameter :
The first element ( must ): The value to fill the array with
The second element ( Optional ): The start position of the fill , The default value is 0
The third element ( Optional ): The end position of the fill , The default is this.length
<> Delete
pop(): Remove the element at the end
shift(): Delete the first element
delete: Delete the specified element , But array length
<> A method to add and delete
splice()
* index: essential . integer , Regulation addition / Delete the location of the item , Use a negative number to specify the position from the end of the array .
* howmany: essential . Number of items to delete . If set to 0, The item is not deleted .
* item1, …, itemX: Optional . New item added to array .
*
<> check
indexOf(): Finds whether an element exists in an array , Return subscript
lastIndexOf() : Finds the last position of the specified element in the array
includes() : Finds whether an array contains an element Return Boolean
find() : Used to find the first qualified array member
<> ergodic
1.forEach():
array.forEach(function(currentValue, index, arr), thisValue)
* currentValue( must ), The value of the current element of the array
* index( Optional ), The current value of the index element
* arr( Optional ), Array object itself
* Unable to exit the loop halfway , It can only be used return Exit this callback , Make the next callback .
* It always returns undefined value , Even if you return I got a value .
* ES6 Three new methods are provided ——entries(),keys() and values()—— Used to traverse arrays .
keys() Is the key name traversal ,values() Is the key value traversal ,entries() Is the traversal of key value pairs . 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"
<> sort
sort()
sort To sort correctly, a sort function must be used
function arrySort(a,b) { return a-b }
<> Reverse the order
reserse()
The return value is undefind
<> merge
1.[…arr1, …arr2, …arr3]
2.concat()
<> Array to string
1.join(): Splits the connection with the specified separator
let a= ['hello','world']; let str=a.join(); // 'hello,world' let str2=a.join(
'+'); // 'hello+world'
2.toString() and join Same, but cannot specify a separator
<> String to array
split() Methods and methods join() The opposite is true
It has two parameters :
* separator essential . String or regular expression , Split from the place specified by this parameter stringObject.
* howmany
Optional . This parameter specifies the maximum length of the returned array . If this parameter is set , No more substrings will be returned than the array specified by this parameter . If the parameter is not set , The entire string is split , Regardless of its length .
If used “” There is no semicolon in the middle , As a separator , Each character is split
<> Array copy
1.const a2 = […a1];
2. Deep copy
* utilize concat let arr = [1,2,3]; let arr2 = [].concat(arr);
* utilize JSON Deep copy let obj = { a:1, b:2, c:undefind, fun:function () { console.log(
"function") } }; let obj2 = JSON.parse(JOSN.Stringify(obj)); console.log(obj2);
//object:[a:1,b:2] // use JSON Copies are ignored undefined and function
<> Dimension reduction of array
flat() Method recursively iterates through the array at a specified depth , All elements and elements in the traversed subarray are merged into a new array to return .
flat(1) It means flattening one floor ,Infinity Keyword can flatten all layers
<>filter() method
filter Method to filter arrays
filter There are three parameters :
element: Elements in an array
index: Parameter location
array: Array itself
usage :
var a = [1,2,2, ,3,4,4,5]; // When only one parameter is written, the element var b = a.filter(function (num) {
return num && num.trim(); //trim() Will remove spaces at both ends of the string }) // You end up with a string without spaces var c = a.
filter(function (num,index,array){ return array.indexOf(num) === index; }) //
In this way, the repeated elements can be removed skillfully , The principle is indexOf Returns where the element first appeared , and index By comparison, we can see whether it is repeated or not
<>ES6 Of set Array de duplication method
The result is to return a new array
let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3]; let set = new Set(array); console.
log(set); // => Set {1, 2, 3, 4, 5}
Technology