This is the third day of my participation in the August Text Challenge.More challenges in August
Basic add and remove
-push - adds one or more elements to the end of the array and returns the length of the array - pop - removes the last bit in the array and returns the deleted value - unshift - adds one or more elements to the beginning of the array, And returns the new length of the array - shift - deletes the first digit in the array, and returns the deleted valueCopy the code
Advanced delete, add, and replace methods
-splice (start[, num, item1, item2... ) - can be used to delete an element at a specified position and add a new element at that position - returns an array of deleted elements - argument - start: -num: indicates the number of elements in the array to be deleted. The default value is 0 or negative, and the element is not removed. Item1, item2... : The contents to add to the arrayCopy the code
Sort an array
- sort (function(a, b){}) - This parameter is optional. By default, characters are sorted according to their Unicode codes. > If the result from A to B is greater than 0, B is ranked before A. > > If the result from A to B is less than 0, A is ranked before BCopy the code
Random sequence
Math.random() - Returns a value between 0 and 1, including 0 and no 1;Copy the code
Common methods for arrays
- concat (arr1, ARR2, arR3...) - Used to merge two or more arrays. This method does not change the array. It returns a new array - join (separator) - concatenates all the elements of an array into a string and returns the string - reverse - reverses all the elements in the array and returns the array. This method changes the original array. -indexof (searchValue[, fromIndex]) - returns the indexOf the first occurrence of the specified element in the array, or -1 if none exists; -lastIndexof (searchValue[, fromIndex]) - Returns the index of the last occurrence of the specified element in the array, or if it does not exist - 1-slice (begin[, end]) - intercepts data from begin to end, And return it as a new array.Copy the code
New array method
-foreach (callback (ele, index, array) [, thisArg]) -foreach (callback (ele, index, array) [, thisArg]) -foreach (callback (ele, index, array) [, thisArg]) Element in array loop -index: the subscript of the element -array: the array currently being manipulated -thisarg: ThisArg = thisArg; thisArg = thisArg; thisArg = thisArg Element in array loop -index: element corresponding subscript -array: array currently being operated on -thisary: - map (callback (ele, index, array) [, thisArg]) - the result of the function executed by each element in the array, as the value of the new array - parameter: - the function executed by the callback -ele: Element in array loop -index: element corresponding subscript -array: array currently being operated on -thisary: Decide that this in callback refers to -reduce (callback (result, ele, index, array) [, initiaValue]) - execute the callback function on each element in the array, Returns a single value based on the condition in the callback function. -parameter: -callback function executed -result: last cumulative result -ele: element being operated on -index: index of the element -array: array being operated on -initiavalue: The initial value of result, if not provided, is used as the first value in the array. -some (callback (ele, index, array) [, thisArg]) -some (callback (ele, index, array) [, thisArg]) -some (callback (ele, index, array) [, thisArg]) -some (callback (ele, index, array) [, thisArg]) -some (callback (ele, index, array) [, thisArg]) -some (callback (ele, index, array) [, thisArg]) Element in array loop -index: element corresponding subscript -array: array currently being operated on -thisary: Determines that this in callback refers to - every (callback (ele, index, array) [, thisArg]) - tests whether all elements in the array pass the test of the specified function, and returns a Boolean. -ele: the element in the array loop -index: the corresponding subscript of the element -array: the array currently being operated on -thisary: Determines the reference of this in the callbackCopy the code