JavaScript Learning notes Finishing Day5

Source: Internet
Author: User
Tags array length

#数组
# #一: definition of an array
1. An array is an ordered set of values
2. Each value is called an element
3. Each element has a position in the array, expressed as a number, called an index. The index starts at 0.
# #二: Features
1. The elements of an array can be of any type.
2. Array index starting from 0, max 2^32-2 4,294,967,294 elements
# #三: Creating an array
1. Using array direct amount []
var arr=[1,2,3];
2. Using constructors
var arr = new Array (10); 10 Elements of Su
var arr = new Array (' A ', ' B ', ' C ');
# #四: The length of the array
Arr.length;
The Length property evaluates only the index of a nonnegative integer
# #五: Access and modification of array elements
1. Use []
2. String of numeric types can also access array elements

# #六: Sparse Array
(sparse arrays are arrays of discontinuous indexes)
1. var arr = new Array (3); --is a sparse array
2. var arr = [,,]; --is a sparse array
3. var arr = [1,2];arr[10]=100; --is a sparse array
4. Delete an element using Delete to get a sparse array
# #七数组元素的添加
1. Assign a value arr[4]=2 to the new index;
2. Insert the new element arr[arr.length]=5 at the end of the array using the array length;
3. Push () adds a new element Arr.push (' abc ') at the tail;
4. Unshift () Add a new element Arr.unshift (' abc ') at the front;
# #八: Deletion of arrays
1. Delete an element after the array delete () is not recommended--produces a sparse array
2. Pop () Delete the last element of the array arr.pop ();
3. Shift () Delete the first element of the array arr.shift ();
# #九: Determine if an array
typeof []; Unable to judge
ESMA5 defines a new method used to determine whether an array: Array.isarray ([]);
# #十: Whether the search array has elements
Arr.indexof ();
If there is a return element position
If there is no return-1
# #十一: Array traversal:
1. Use for loop traversal
for (var i = 0; I < arr.length;i++) {
Console.log (Arr[i]);
}

2. Using the for/in loop
for (var i in arr) {
Console.log (Arr[i]);
}
3. Note: The difference between traversing a sparse array and a special subscript array </font>
4. Use foreach (); Incoming anonymous function
> <font color= "Pink" > Low version IE does not support </font>



# #十二: Array of methods:
1. Join () to stitch the elements of an array into a string
var message = Arr.join (', ');
2. Concat () merging multiple arrays
var message = Arr.concat ([12,43],[4,35,3]);
var message = Arr.concat (n/a);
var message = Arr.concat (1,2,[4,,5,[6,,7]]);
3. Slice () intercepts a portion of the array (without destroying the original array) and returns the new number
var arr = List.slice (2); Remove all elements from subscript 2 to the back
var arr = list.slice (2,4); Remove elements from subscript 2 to subscript 4 (4 not included) >
4. Splice () Delete, replace, add, destroy the original array to return deleted content
var New_arr = List.splice (2); Elements after subscript 2
var New_arr = List.splice (2,2); Two elements after subscript 2
4.1 Adding a new element at the specified location
var arr = [3,454,435,343];
Arr.splice (2,0, ' abc ');
Add an ABC in front of the 435 position
4.2 Replace the element at the specified position
var arr=[1,2,3,4];arr.splice (2,1, ' abc ');
Replace 3 with ABC;
5. Reverse () array inversion change the original array

6. Sort (); Array ordering, you must use the sort function list=[1,334,32,324];
List.sort (function (A, b) {return-A;});
7. ToString () converts an array to a string

8. toLocaleString () converts an array to a local string (not commonly used)

JavaScript Learning notes Finishing Day5

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.