Hyper-detailed JavaScript array-related rollup __javascript

Source: Internet
Author: User
Tags array length javascript array
Preface

In Rn involves more array of related operations, so here is a summary of this piece of content, but also to this knowledge of learning.
The first part of this article is sorted according to the rookie tutorial . Create an array

There are generally three ways to create an array:

var array = new Array ();
var array = new Array (length);
var array = new Array (element0, element1, ..., elementn);
or
var array = [Element0, Element1, ..., ELEMENTN];

Array () is a constructor and generally uses the new array () to return the newly created and initialized array. When it is called as a function call, it is called with the new operator.

When the first argument is not available, an empty array is returned.
Second argument , this argument represents the length of the array you want to create, and returns an empty array of length for the specified argument.
Like what:

Array (3)
//[,,,]

The third parameter (at least 2)that represents the elements in the array, and returns an array of those parameters. Property

Property There's nothing to say, there are 3 properties below: Constructor returns a reference to the array function that created this object. Length Sets or returns the number of elements in the array. Prototype adds properties and methods to an array object. Method

Concat () connects two or more arrays and returns a new array .
Syntax: Array1.concat (array2,array3,..., Arrayx)
A parameter can be one or more arrays, or it can be one or more elements.
Note: Array1 does not change and returns a new array.

Copywithin () copies the element from the specified position of the array to another specified position in the array, returning the current array .
Syntax: Array.copywithin (target, start, end)
The first parameter target is required. Copy to the specified target index location.
The second argument start is optional. The starting position of the element copy. (default is 0) if negative, indicates reciprocal.
The third argument end is optional. Stop reading data before this location (default is Array.Length) if negative, indicates reciprocal.
Note: This method is ES6 new to join.

Every () detects whether each element of a numeric element meets the criteria. returns TRUE or false and does not change the elements in the current array .
Syntax: Array.every (function (Currentvalue,index,arr), Thisvalue)
First, the first parameter of every () is a function, that is, the specified condition. This parameter is required. Each element in the array executes this function to detect whether the condition is eligible. If an element is detected in an array that is not satisfied, the entire expression returns false and the remaining elements are no longer instrumented. Returns true if all elements satisfy the condition. the first argument in a function currentvalue required. Represents the value of the current element. The second parameter index is optional. The index value of the current element. The third parameter arr optional. The array object to which the current element belongs.
Then the second parameter of every () Thisvalue is optional. object is used as the execution callback, passed to the function, to bind the callback function to the This object. If Thisvalue is omitted, the value of "This" is "undefined"

Fill () fills the array with a fixed value. returns the current array .
Syntax: Array.fill (value, start, end)
The first parameter value is required. The value of the fill.
The second argument start is optional. Start the fill position. (Default is 0)
The third argument end is optional. Stops the fill position (not included). (Default is Array.Length)
Note: This method is ES6 new to join.

Filter () detects numeric elements and returns a new array of all elements that match the criteria.
Syntax: Array.filter (function (Currentvalue,index,arr), Thisvalue)
All parameters in this method are the same as the Every () method.

Find () returns the first array element that matches the incoming test (function) condition. If not, return to undefined. does not change the original value of the array.
Syntax: Array.find (function (currentvalue, index, arr), Thisvalue)
All parameters in this method are the same as the Every () method.
Note: This method is ES6 new to join.

FindIndex () returns the index of the first array element that conforms to the incoming test (function) condition. If not, return-1. does not change the original value of the array.
Syntax: Array.findIndex (function (currentvalue, index, arr), Thisvalue)
All parameters in this method are the same as the Every () method.
Note: This method is ES6 new to join.

Each element of the ForEach () array performs a callback function that is commonly used to traverse the elements in the array .
Syntax: Array.foreach (function (currentvalue, index, arr), Thisvalue)
All parameters in this method are the same as the Every () method.

IndexOf () Searches the elements in the array and returns the location of the first. Returns 1 if the element is not.
Syntax: Array.indexof (Item,start)
The first argument item must be. The element to find.
The second element start Optional integer argument (default is 0). Specify where to start retrieving in the string. Its legal value is 0 to array.length-1.

Join () puts all the elements of an array into a string. returns the converted string
Syntax: Array.join (separator)
Parameter separator is optional, specifying the delimiter to use. If this argument is omitted, commas are used as delimiters.

LastIndexOf () returns the last occurrence of a specified string value, which is searched forward at the specified position in a string.
Syntax: Array.lastindexof (Item,start)
The parameter is the same as indexof (), but the default value for the second argument start is array.length-1 because it is searched backwards.

Map () Handles each element of the array by specifying the function, and returns the processed new array without changing the value of the source array
Syntax: Array.map (function (Currentvalue,index,arr), Thisvalue)
All parameters in this method are the same as the Every () method.

Pop () deletes the last element of the array and returns the deleted element. changes the length of the original array .
Syntax: Array.pop ()

Push () adds one or more elements to the end of the array and returns a new length. changes the length of the original array .
Grammar: Array.push (item1, item2, ..., itemx)

Reduce () evaluates the array element to a value (left to right).
Syntax: Array.reduce (Function (total, CurrentValue, Currentindex, arr), InitialValue)
Reduce () receives a function as an accumulator, and each value in the array begins to shrink from left to right and finally evaluates to a value.
The first parameter is required for function (Total,currentvalue, Index,arr). The function used to execute each of the array elements. The first argument in the function total is required. The initial value, or the return value after the calculation is completed. The second parameter currentvalue required. The current element. The third parameter Currentindex optional. The index of the current element. The fourth parameter arr is optional. The array object to which the current element belongs.
The second parameter InitialValue optional. The initial value passed to the function.

Reduceright () evaluates the array element to a value (from right to left).
Syntax: Array.reduceright (Function (total, CurrentValue, Currentindex, arr), InitialValue)
This method parameter is the same as reduce (). The difference is that it sums up the array items in the array from the end of the array.

Reverse () Reverses the order of the elements of the array. reverses the order of the original array elements.
Syntax: Array.reverse ()

Shift () deletes and returns the first element of the array. changes the length of the original array .
Syntax: Array.shift ()

Slice () selects a portion of the array, returns a new array, and does not change the value of the original array . Used to intercept arrays.
Syntax: Array.slice (start, end)
The first parameter start is required. Specify where to start the selection. If it is a negative number, then it sets the position from the end of the array. In other words,-1 refers to the last element, 2 to the penultimate element, and so on.
The second argument end is optional (the default is the array length). Specify where to end the selection (not included). The parameter is the array subscript at the end of the array fragment. If this argument is not specified, the Shard array contains all the elements from start to the end of the array. If this argument is a negative number, it sets the element that starts at the end of the array.

Some () detects if there are elements in the array element that meet the specified criteria. returning Ture or false does not change the elements in the current array .
Syntax: Array.some (function (Currentvalue,index,arr), Thisvalue)
All parameters in this method are the same as the Every () method.
But we need to be aware of the difference with the Every () method:
The Some () method executes each element of the array sequentially: if one element satisfies the condition, the expression returns true and the remaining elements do not perform the detection again. Returns False if no element satisfies the condition.

Sort () The elements of the array are sorted. will change the original array .
Syntax: Array.Sort (sortfunction)
The parameter sortfunction is optional and is a function to specify the sort order.
The sort order can be letters or numbers, and in ascending or descending order. The default sort order is alphabetical ascending.

Splice () adds, replaces, or deletes an element from an array. will change the original array .
Syntax: Array.splice (index,howmany,item1,....., itemx)
The first parameter index is required. Specify where to add/remove elements from. This parameter is the subscript for the array element that starts inserting and/or deleting , and must be a number.
The second parameter howmany required. Specify how many elements should be deleted . Must be a number, but it can be "0". If this parameter is not specified, all elements that start from index to the end of the original array are deleted.
The third parameter item1, ..., itemx Optional. The new element to add to the array.

ToString () converts the array to a string and returns the result. The elements in the array are separated by commas.
Syntax: array.tostring ()

Unshift () adds one or more elements to the beginning of the array and returns a new length. changes the length of the original array .
Grammar: Array.unshift (item1,item2, ..., itemx)

ValueOf () returns the original value of the array object. does not change the original array.
Syntax: array.valueof ()
The valueof () method returns the original value of the Array object.
The original value is inherited by all objects derived from the Array object.
The valueof () method is usually invoked automatically in the background by JavaScript and does not appear explicitly in the code. the expansion of arrays in ES6

In addition to the new additions to the ES6 mentioned in the top method, there are some other extensions to the array, and below is a note based on the extension of the Nanyi's ECMAScript 6 Getting Started array. extension Operators

The extension operator (spread) is a three-point ..., mainly for function calls, which function to change an array into a sequence of arguments .
Like what:

Console.log (.....) [1, 2, 3])
1 2 3

console.log (1, ...) [2, 3, 4], 5)
//1 2 3 4 5

function push (array, ... items) {
  Array.push (... items);
}

Because the extension operator can expand an array, you no longer need the Apply method to convert the array to a function argument.

ES5
Math.max.apply (NULL, [3, +])

//ES6 writing
Math.max (... [3, +])

Equivalent to
Math.max (14, 3, 77);

Other relevant content, you can go to the top link to view. Array.from ()

The Array.from method is used to convert two classes of objects into real arrays : objects that resemble arrays (Array-like object) and Traversal (iterable) objects (including ES6 new data Structures Set and MAP).
Like what:

Let Arraylike = {
    ' 0 ': ' A ',
    ' 1 ': ' B ',
    ' 2 ': ' C ',
    length:3
};

ES5 the writing of
var arr1 = [].slice.call (arraylike);//[' A ', ' B ', ' C ']

//ES6 's writing let
arr2 = Array.from (arraylike ); [' A ', ' B ', ' C ']

In addition to the above extension operator ..., it is possible to convert an object that deploys the iterator interface (Symbol.iterator) to an array. The Array.from method also supports objects that resemble arrays. So-called objects of similar array, the essential feature is only one point, that is, there must be a length attribute. Therefore, any object with a length property can be converted to an array by the Array.from method, while the extension operator cannot be converted.

Array.from can also accept the second parameter, which acts like an array of map methods, which is used to process each element and put the processed value into the returned array.

Array.from (arraylike, x => x * x);
Equivalent to
array.from (arraylike). Map (x => x * x);

Array.from ([1, 2, 3], (x) => x * x)
//[1, 4, 9]
Array.of ()

The Array.of method is used to convert a set of values to an array. The main purpose of this method is to make up for the inadequacy of array constructor arrays (). Because of the difference in number of parameters, the behavior of array () is different.
Array.of can basically be used instead of array () or new array (), and there are no overloads that result from different parameters. Its behavior is very uniform.

Array.of ()//[]
array.of (undefined)//[undefined]
array.of (1)//[1]
array.of (1, 2)//[1, 2]

Array.of always returns an array of parameter values. If there are no arguments, an empty array is returned. entries () of an array instance, keys (), and values ()

ES6 provides three new methods--entries (), keys (), and values ()--for traversing arrays. They all return a traversal object that can be traversed with a for...of loop, except that the keys () are traversal of key names, values () are traversal of key values, and entries () is the traversal of key-value pairs.
Like what:

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"
includes () of an array instance

The Array.prototype.includes method returns a Boolean value that indicates whether an array contains a given value, similar to the includes method of the string. ES2016 introduced the method.

[1, 2, 3].includes (2)     //True
[1, 2, 3].includes (4)     //False
[1, 2, Nan].includes (NaN)/True

The second parameter of the method represents the starting position of the search, and the default is 0. If the second argument is a negative number, it indicates the position of the reciprocal, and if it is larger than the array length (for example, the second argument is-4, but the array length is 3), it is reset to start from 0.

[1, 2, 3].includes (3, 3);  False
[1, 2, 3].includes (3,-1);//True
Related Article

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.