Accumulation of array-related methods (VUE\AG, etc.)

Source: Internet
Author: User

Change the original array:

    1. Shift: Delete the first element and return the deleted element, empty is undefined
    2. Unshift: Adds an element to the beginning of the array and returns the new length
    3. Pop: Delete the last one and return the deleted element
    4. Push: Adds an element to the end of the array and returns the new length
    5. Reverse: Reverse Array order
    6. Sort: Sorting an array
    7. Splice:splice (start,length,item) Delete, increment, replace array element, return the deleted array, no delete does not return

Do not change the original array:

    1. Concat: Concatenate multiple arrays, returning a new array
    2. Join: Place all elements in an array with a parameter as a delimiter into one character
    3. Slice:slice (Start,end), returns the selected element, returns the selected element from an existing array, returns from the start bit to end (including start excluding end) if it is a negative number, Represents the calculation from the end of the array (again, including start, which does not include ends), note that the method does not modify the array, but instead returns a sub-array
    4. Map,filter,foreach,some,every, etc. do not change the original array

--------------------------------------------------------------------------------------------------------------- --------------

1. Join ()

 功能:将数组中所有元素都转化为字符串并连接在一起。 输入: 分隔符,默认为逗号 输出:分隔符分割的字符串 举例:
var a = [1,2,3];a.join(); // =>"1,2,3"a.join(";");// =>"1;2;3"

2, Reverse ()

功能:将数组中的元素颠倒顺序。输入:无输出:逆序的数组举例:
var a = [1,2,3];a.reverse.join()// =>"3,2,1"

3. Sort ()
Function: The elements in the array
Input: comparison function or null. Null means sorting in alphabetical order, passing in a comparison function with two parameters, returning a number less than 0 before the first argument, and returning an array greater than 0 after the first argument
Output: Array after sorting
Note: The original array was changed
Example:

var a = [22,33,11111,4444];a.sort();// =>11111,22,33,4444a.sort(function(a,b){ return a-b;});// => 22,33,4444,11111a.sort(function(a,b){return b-a})// =>11111,4444,33,22

4, Concat ()
Function: function of array stitching
Input: The element to be spliced; if there is an array in the argument, the array element is concatenated, not the array itself, but recursion is not supported and the array of calls is not modified.
Output: A new array after stitching
Note: The new array, the original array does not change.
Example:

var a = [1,2,3];a.concat(4,5) // =>[1,2,3,4,5]a.concat([4,5]) // =>[1,2,3,4,5]a.concat(4,[5,[6,7]]) //[1,2,3,4,5,[6,7]]

5, Slice ()
Function: Gets the fragment or sub-array of the original array
Input: Start and end of fragment
Output: The returned array contains the position specified by the first parameter and all elements that are between and without the specified position of the second parameter. If negative, indicates the position relative to the last element in the array.
Note: The new array, the original array does not change.
Example:

var a = [1,2,3,4,5];a.slice(0,3);// =>[1,2,3]a.slice(3);// =>[4,5]a.slice(1,-1)// =>[2,3,4]

6, Splice ()
Function: Removes an element from an array, inserts an element into an array, or a colleague completes both operations.
Input: The first parameter is the starting position for the specified insertion or deletion, and the second parameter is the number to delete. The following parameters represent the elements that need to be inserted into the array
Output: Returns an array consisting of the deleted elements.
Note: A new array was created and the original array was modified
Example:

var a = [1,2,3,4,5,6,7,8];a.splice(4) // =>[5,6,7,8];a [1,2,3,4]a.splice(2,0,‘a‘,‘b‘) // =>[];a [1,2,a,b,3,4]

7. Push () and pop ()
Function: Push adds one or more elements from the end of the array and returns the new length of the array; pop returns the last element
Input: Push input is an inserted element; pop input is empty
Output: Push is the new length; the pop is the returned array element
Example:

stack = [];stack.push(1,2);// =>stack:[1,2],返回2stack.pop;// => stack: [1],返回2

8, Unshift () and shift
Function: Similar to push and pop, the difference is inserted and deleted from the head, not the tail

9, ToString () and toLocaleString ()
Function: Converts each element of an array to a string, and enters a comma-delimited list of strings. function similar to join ();
Input: None
Output: String
Example:

[1,2,3].toString() // =>‘1,2,3‘

*********** The following are the 9 new methods added in ES5 *********
10. ForEach ()
Function: Iterate through the array and invoke the specified function for each element
Input: Input is a function to be traversed, the parameters of the function are: array elements, the index of the elements, the array itself
Output: Only the traversal function is executed, no specific return
Example:

var data = [1,2,3,4,5];data.forEach(function(v,i,a){a[i] = v + 1;});data // =>[2,3,4,5,6]

11. Map ()
Function: Each element of the called array is passed to the specified function, and a new array is returned
Input: Same as foreach
Output: A new array that finishes executing functions
Note: Returns the new array without modifying the original array
Example:

a = [1,2,3];b = a.map(function(x){return x*x;}); // =>[1,4,9]

12. Filter ()
Function: Performs a specific function on the array element and returns a subset.
Input: A function that performs a logical judgment, which returns TRUE or FALSE, with parameters similar to foreach and map ()
Output: If the Execute input function returns a value of True, the element passed to the decision function is a member of this subset
Example:

a = [5,4,3,2,1];smallvalues = a.filter(function(x){return x<3});// =>[2,1]

13, every () and some ()
Function: The logical judgment of the specified function for the array element.
Input: Specific function
Output: TRUE or False
Example:

var a = [1,2,3,4,5];a.every(function(x){return x<10;}) // =>true

14. Reduce () and reduceright ()
Function: Uses the specified function to combine array elements to produce a single value.
Input: Two parameters, the first is a function to perform the simplification operation. The second (optional) parameter is an initial value that is passed to the function.
Note: The first parameter of the function that performs the simplification is the result of the simplification operation accumulated so far.
15, IndexOf () and LastIndexOf ()
Function: Searches the entire array for the given element of merit, returns the index of the first element found, or returns -1.LASTINDEXOF as a reverse lookup if it is not found.
Input: The value of the element.
Input: Index value

************************* Ornate split ES6 extension *************************
16, Array.from ()
Function: Convert two types of objects to real arrays: arrays-like objects and objects that can be traversed
Input: The object to be converted, the second parameter is optional, acts like an array of map methods, used to process each element, put the processed value into the returned array.
Input: Array
Example:

let arrayLike = {       ‘0‘:‘a‘,       ‘1‘:‘b‘, ‘2‘:‘c‘, length:3};let arr2 = Array.from(arrayLike);// =>[‘a‘,‘b‘,‘c‘]

17, Array.of ()
Function: Converts a set of values into an array.
Input: Array element
Output: Array
Example:

Array.of(2,11,3)// =>[2,11,3]Array.of(3)// =>[3]

18, Copywithin ()
Function: Copies the members of the specified position to another location within the current array, returning the current array.
Input: The first parameter (the data is replaced from this position), the second parameter is optional (starting from that position, the default is 0, the negative value is the reciprocal); the third parameter (stop reading before the position, default is the array length)
Output: Returns the currently replaced array.
Note: The current array is changed
Example:

[1, 2, 3, 4, 5].copyWithin(0, 3) //=> [4, 5, 3, 4, 5]

19. Find () and FindIndex ()
Function: Finds the first array member that matches a condition.
Input: callback function, where all array members execute the function sequentially until they find the first member to return a value of true. The callback function can accept three parameters, followed by values, positions, and the original array.
Output: Find () returns the found member; FindIndex () returns the position of the member.
Example:

[1,5,10,15].find(function(value,index,arr){ return value > 9;})// =>10

20. Fill ()
Function: Fills an array with the given value.
Input: The first parameter is the value to be filled, and the second and third parameters are optional, representing the starting and ending positions of the fill, respectively (not included).
Output: Populated array
Example:

[‘a‘, ‘b‘, ‘c‘].fill(7, 1, 2)// =>[‘a‘, 7, ‘c‘]

21, Entries (), keys (), VALUES ()
Function: Used to iterate through an array, which can be traversed with a for...of loop. The difference is that keys () is the traversal of the key name, values is the traversal of the key value, and entries () is the traversal of the key-value pair.
Input: None
Output: Walker object
Example:

for (Let indexof [' A ',' B '].keys ()) {Console.log (index);}//=0//=1for (Let Elemof [ ' a ',  ' B '].values ()) {console.log (Elem);} // => ' a ' // => ' B ' for (let [index, Elem] of [ ' a ',  ' B '].entries ()) {console.log ( index, elem);} // =>0  "a" // = 1  "B"          

If you do not use the For...of loop, you can manually invoke the next method of the Walker object for traversal.

22, includes ()
Function: Indicates whether a number group contains the given value
Input: The first parameter must be selected (the given value to be checked), the second parameter is optional, which indicates the starting position of the search, default is 0, negative numbers indicate the position of the reciprocal.
Output: A Boolean value.
Note: In contrast to the indexof, IndexOf makes a strong pair of operators, which results in a miscarriage of the Nan.
Example

[1, 2, 3].includes(2);     // true[1, 2, 3].includes(4); // false[1, 2, NaN].includes(NaN); // true


array.indexOf(searchElement[, fromIndex = 0])
Returns the first index value that a specified element can find in the array, otherwise returns 1
Fromindex can be negative, which means starting from the penultimate nth ( at this point still querying the array from the front)
Match with "strict equality" (= = =)

An example:

          var obj = {a‘test‘}          var arr = [‘a‘‘b‘, {a‘test‘}]          console.log(arr.indexOf(obj))     //-1          var arr2 = [1‘b‘, {a‘test‘}]          console.log(arr2.indexOf(‘1‘))      //-1

array.lastIndexOf(searchElement[, fromIndex = arr.length - 1])
Returns the last index of the specified element in the array, or 1 if it does not exist, looking forward from behind the array

Accumulation of array-related methods (VUE\AG, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.