The array in JS

Source: Internet
Author: User
Tags string format

the array in JS
1, the basic concept of the array?
An array is a collection of sequential arrays that are stored continuously in memory space.
The order of the elements in the array, called subscripts. Each element of an array can be accessed as an subscript.
2, how to declare an array?
1) declare var arr=[] with an argument;
in JS, the same array can store various data types.
For example:

2) Use the New keyword declaration:


the parameters in the >>> brackets can be:
A. Parameter omitted, representing an empty array
B. The parameter is an integer that declares an array of length, but the length can be appended at any time.

c. A comma-separated number of values that represent multiple values of an array.
new Array (==[1,2,3)
3, read/write/delete elements in the array?
1) Read and write: Access the element by subscript. Subscript starting from 0 arr[1]= "haha";
2) Additions and deletions:
A. Use the Delete keyword to delete one of the values in the array. After deletion, the length of the array is unchanged, and the corresponding position becomes undfined.
eg:

B.arr.pop (): Deletes the last value of the array, equivalent to arr.length-=1;
C.arr.shift (): Delete the first value of the array;
D. Arr.unshift (value): Add a value in the No. 0 position of the array;
E.arr.push (value): Adds a value to the last position of the array;
F. Direct access to an array of subscript not reached, can be dynamically appended.
Arr[100]=1, the middle if there is empty remaining mark, will be deposited undfined.
4. Other methods in the array:
1) jion (): Concatenate the array with the specified delimiter as a string. When the argument is empty, the default is separated by commas. "The original array does not change", so to define a new element

2) Concat: "The original array will not be changed" array, connecting the array to multiple arrays as a new array.
Concat If you have a two-dimensional array when connecting, you can remove up to one layer []
[1,2].concat ([3,4],[5,6])-->[1,2,3,4,5,6]
[1,2].concat (3,4,[5,6])-->[1,2,3,4,[5,6]
3) The push () array finally adds one to the beginning of the Unshift () array, and returns the length of the new array.
The pop () array finally deletes one, and the Shifi () array begins by deleting one,-returning the deleted value.
"The above method will change the original array"
4) Reverse (): "The original array is changed" reverses the array and outputs in reverse.
5) Slice (begin,end): "The original array will not be changed" intercepts a part of the array and returns a new array that is intercepted.
>>> passes in a parameter that represents the start interval, which is truncated to the end of the array by default.
>>> Incoming two parameters, indicating the start and end subscripts, left closed right open interval (contains begin does not contain end).
>>> Two parameters can be negative, which means the number starts from the right and the last value is-1.
6) sort (): [The original array is changed] the array is sorted in ascending order;
>>> By default, the ASCII value is sorted by the first letter of each element;
[3,1,5,12].sort ()->[1,12,3,5];
>>> can pass in a comparison function, manually specify the sorting function algorithm;
The function will receive a value of two by default, and if the function value is >0, the a>b is proved.
Arr.sort (function (b) {
return a-B; //ascending order
return b-a; //Descending order
});
7) IndexOf (Value,index): Returns the subscript where the first value value in the array is located, if no return-1 is found;
lastIndexOf (Value,index): Returns the subscript where the last value value in the array is located, if no return-1 is found;
>>> If index is not specified, all groups are searched for value;
>>> If index is set, the value is searched backwards, starting with index.
8) ForEach (): Designed to iterate through an array. Receive a callback function, the callback function receives two parameters, the first parameter is each item value of the array, and the second parameter is the subscript.
"This function is not supported before IE8 "
Arr.foreach (function (item,index) {
Console.log (item);
});
9) Map (): Array map. Using the same method as foreach (), the map can have a return value, indicating that each value of the original array is manipulated and returned to a new array.
"This function is not supported before IE8 "
var arr1=arr.map (function (item,index) {
Console.log (item);
return item-1;
});
5. Two-bit arrays with sparse arrays (learn)
1) Two-bit array: The values in the array are still in the array form.
eg:arr=[[1,2,4],[3,4,5]];//equivalent to two rows of three columns
read two-bit array: arr[[column];
2) Sparse array: The index in the array is discontinuous. (length is greater than the actual number of elements in the array)
6. Basic data Types and reference data types
1) Basic data type: When assigning a value, the value in the original variable is assigned to another variable, two variables are independent of one another, and the other does not change.
2) Reference data type: When assigning a value, it is the address of the original variable in memory, assigned to another variable. When the assignment is complete, the same memory address is stored in two variables, the same data is accessed, and one of the changes is changed.
3) variables, such as numeric, String, and Boolean, belong to the basic data type.
second, Array object
1. Boolean class
There are two ways of declaring it:
1) A simple variable can be declared using an argument, and a Boolean Boolean type is detected with TypeOf.
2) You can also use the new Boolean () to declare an object of type Boolean. The object type is detected with TypeOf.

2. Number class
Number.MAX_VALUE returns the maximum value that the number class can represent;
Number.min_value returns the minimum value that the number class can represent .
. ToString (): Converts a numeric type to a string type;

. Tolocalstring (): Converts a numeric value to a string in the local format order, usually starting from the right, with three groups separated by commas;

. toFixed (N): preserves the string n decimal places and preserves the string format (common);

. Toprecision (N): Formats the number as a specified length, n indicates the length of digits without a decimal point, if n< the original number length;
is represented by scientific notation. If n> the original number length, then 0 after the decimal point;

. VALUEOF (): Returns the base numeric value of the number object.

3. String class
1) Property: Str.length Returns the length of the string, the number of characters
The string supports an array-like subscript access method: Str[0];
2) Method:
. toLowerCase (): Converts all characters of a string to lowercase

. toUpperCase (): Converts all characters of a string to uppercase

. CharAt (N): Intercept the nth character of an array, equivalent to Str[n]

. indexOf ("str", Index): Starts at the index position, finds the position of the substring in the string, and the day has not returned-1,
Other array indexof methods:. LastIndexOf ();
. substring (begin,end): Intercepting a string from a string

>>> passes only one parameter, representing the beginning of begin to the last
>>> Pass in two parameters, representing the interval from begin to end, left closed and right open.
. Split ("delimiter"): separates the string into the array with the specified delimiter, passing in an empty "" means separating each character of the string into the array
. Replace ("old", "new"): Replace the first old in a string with the new
>>> The first parameter, either a normal string or a regular expression
>>> If it is a normal string, only the first old is replaced, and if it is a regular expression, it can be replaced by the notation of the regular expression.

4. Date Dates class
1, new Date (): Returns the current latest time;
New Date ("2017,8,23,12:34:04"); Returns the specified time

2. Common methods:
. getFullYear (): Get 4-bit year
. GetMonth (): Get Month (0-11)
. GetDate (): Gets the day of the first one months 1-31
. GetDay (): Gets the day of the week 0-6,0 represents Sunday
. GetHours (): When getting
. getminutes (); Get points
. getseconds (): Get seconds




The array in JS

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.