JS arrays and built-in objects

Source: Internet
Author: User
Tags array length

[the array in JS]

1. The concept of arrays: arrays are structures that store multiple sequential elements in memory
The order of the elements, called subscripts, is to find the corresponding element by subscript.

2, the Declaration of the array:
① literal declaration: var arr1 = [];
JS in the same array, you can store many different data types (but generally the same array is used only for the same data type):
For example: var arr1 = [1, "2", true,{"name": "Jianghao"},[1,2]];

②new keyword declaration: var arr2 = new Array (parameter);
The >>> parameters can be:
A. Null: Declares an array with no specified length;
B. Length of the array: declares an array of a specified length, but the length of the array can be appended at any time, with a maximum length of 0-(2^32-1);
C. Array default n values: New Array (1, "2", true); equivalent to [1, "2", true]

3. Read/write/delete of elements in array:
① Read and write: Access to elements by subscript, e.g. arr[2];
② additions and deletions: delete arr[n]; Delete the array n+1 value, but the array length is unchanged, the corresponding position value is undefined
Arr3.push (value); The array finally adds a value equivalent to arr3[arr3.length] = 7;
Arr3.unshift (value); The No. 0 bit of the array inserts a value and the remaining digits are postponed;
Arr3.pop (); Delete the last array, unlike the delete, the array length will be reduced by one after the pop execution; equivalent to arr.length-=1;
Arr3.shift (); Delete the No. 0 bit of the array, and the length will be reduced by one;

4. Other common methods in the array:
①join ("delimiter"); Separates the array with the specified delimiter, linked as a string. When the parameter is empty, the default is separated by commas;
②concat (); "The original array is not altered" to link the array to the values of multiple arrays as a new array;
[1,2].concat ([3,4],[5,6]) =[1,2,3,4,5,6]; When connected, the brackets are at most one layer apart
[1,2].concat ([1,2,[3,4]]) =[1,2,1,2,[3,4]];//multi-layered bracket, in two-dimensional array form

③push (): The last increment of the array; Unshift (): Array begins adding a number "returns the new length of the array"
Pop (): delete array last; SHITF (): Delete array First "return deleted value"
"Call the above method, the original array is changed"

④recerse (): "Original array Changed" array flipped, sorted in reverse order.

⑤slice (Begin,end): "The original array will not be altered" to intercept a part of the array as a new array
>>> pass a parameter: The default is begin index, and when this starts, the last one is truncated;
>>> Pass two parameters: intercept from begin to end interval, left closed right open (contains begin, does not contain end);
The >>> parameter can be negative,-1 means the last one;

⑥sort (function): "The original array will be changed" to sort the arrays;
>>> do not specify sorting function: Rehearse according to the value of ASCII code;
>>> incoming sort function: default two parameters A, B, if the return value >0, a>b; returns the value <0, then a<b
Arr3.sort (function (b) {
return a-B; b in front, a in the back (ascending order)
return b-a; A in front, b in the back (descending order)
});

⑦indexof (Value,index): Returns the subscript position corresponding to the first value value in the array, if no return-1 is found
LastIndexOf (Value,index): Returns the subscript position corresponding to the last value value, ...
>>> if the index parameter is not specified: The default is to query all elements of the array;
If you specify the index parameter: start with the current index and query backwards;


5, two-dimensional arrays & sparse arrays (Learn):
① sparse array: The array does not contain all indexes starting from 0 to length-1 (the length value is more than the actual element number);
② two-dimensional array: var arr = [[1,2],[3,4],[5,6]]; Matrix equivalent to three rows and two columns
Take out a two-dimensional array element: arr[[column number]; You can use nested loops to traverse

6. Reference data type & basic data type:
Reference data type: When assigning a value (array/object), the address of the original variable is assigned to the new variable. Two variables, actually manipulating the same data, so modify one of the variables, and the other changes;
Basic data type: When assigning a value, the value of the original variable is assigned to the new variable. Two variables, which belong to different memory space, modify one, do not interfere with each other;


"Built-in objects"

Number class

Number.min_value the minimum number that can be represented
Maximum number of number.max_value that can be represented
. ToString (): Converts a number to a string, equivalent to num+ ""
. toFixed (N): Converts a number to a string, leaving N decimal places, rounding
. VALUEOF (): Returns the base numeric value of the number object
. toLocaleString (): Converts numbers into strings in the order of local formatting. Generally, three a group with commas
. Toprecision (N): Formats a number as a specified length, n= all digits without a decimal point, and

Strings string
1. Properties: Str.length Returns the length of a string
2. Method:
. toLowerCase () All characters are converted to lowercase;
. toUpperCase () Convert all characters to uppercase;
. CHARAT (n) intercepts the nth character in a string
. INDEXOF ("Query substring", index) queries the index of the first substring starting at index. No return-1, indexof () method of the same array was found;
. substring (begin,end) intercept sub-string
>>> write only one parameter: Starting from begin, to the last
>>> write two parameters: Beginning with begin, to end. Left and right open.
. replace ("old", "new") replaces the first old in the string with the new one. The first argument can be a normal string, or it can be a regular expression (the normal string will only match the first one, and the regular expression is differentiated according to the specific case).
. Split ("") divides the string into arrays by making delimiters. An empty string is passed in, and a single character is stored in the array

Date class
1, new Date (); Get the current time of the latest;
The new date ("2017,4,4,14:58:12") sets the specified time
2. Common methods:
. getFullYear (): Get 4-bit year
. GetMonth (): Get month 0~11
. GetDate (): Get a day in January 1~31
. GetDay (): Get a day of the week 0~6
. GetHours () returns the hour of the date object (0 ~ 23)
. getminutes () returns the minute of the Date object (0 ~ 59)
. Getseconds () returns the number of seconds for a Date object (0 ~ 59)

JS arrays and built-in objects

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.