js's built-in object type------Array object

There are many kinds of objects in js, commonly used objects are: String, Math, Array, Date.
Because the String object was mainly introduced in the previous blog, the Array object is mainly introduced here.

Array objects
Array objects can not only store data, but also have a large number of methods and properties for manipulating the data in the array.
create:
new Array();
new Array(size);
new Array(element0, element1, ..., elementn);
var myArray = [];

The parameter size is the desired number of array elements. For the returned array, the length field will be set to the value of size.
Parameters element ..., elementn is a list of parameters. When the constructor Array() is called with these parameters, the elements of the newly created array are initialized to these values. Its length field is also set to the number of arguments.
In daily ordinary programming, I mainly use the fourth method.
Return Value
Returns the newly created and initialized array.
If the constructor Array() is called with no arguments, the returned array is empty and has a length of 0.
When the constructor is called with only one numeric parameter, the constructor will return an array of the specified number of elements with undefined elements.
When Array() is called with additional parameters, the constructor will initialize the array with the values ​​specified by the parameters.
When the constructor is called as a function, without the new operator, it behaves exactly the same as when it is called with the new operator.
Properties:
Has three properties:
constructor Returns a reference to the array function that created this object.
length sets or returns the number of elements in the array.
prototype gives you the ability to add properties and methods to an object.
Methods:
There are many methods:
concat() Concatenates two or more arrays and returns the result.
join() puts all elements of an array into a string. Elements are separated by the specified delimiter.
pop() removes and returns the last element of the array
push() adds one or more elements to the end of the array and returns the new length.
reverse() reverses the order of elements in an array.
shift() removes and returns the first element of an array
slice() returns the selected element from an existing array
sort() sorts the elements of an array
splice() removes an element and adds a new element to the array.
toSource() returns the source code for this object.
toString() converts an array to a string and returns the result.
toLocaleString() converts the array to a local array and returns the result.
unshift() adds one or more elements to the beginning of the array and returns the new length.
valueOf() returns the original value of the array object
New methods:
After 2005, Array has added 7 new methods:
indexOf and lastindexOf Find an array element (similar to the String object's indexOf and lastindexOf) Return the first element in the array The index of the occurrence position and the last occurrence position.
The remaining 5 methods are called iterative methods because they iterate (loop) the array.
every(),some(),filter(); test each element, the first two are test methods, every is to test whether all elements in the array pass the test in the function. some is to only test whether some elements in the array pass the test; the filter method is to execute a function for each element in the array, if the function returns true for an element, add the element to the in another array returned by the filter method.
forEach and map somehow use the elements in the array to perform some kind of operation. The difference is: the map operation will store the result of each operation in another array returned by the map method.
It is important to note that the functions used by these five methods must follow a rule: the function must accept 3 parameters. ! ! !














Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802824&siteId=291194637