Array objects in JavaScript

Source: Internet
Author: User

1. Three ways to create an array: 1.1 var array name =[element 1, Element 2, Element 3 ...];

For example:

var arr1=[1,2,3,4];
1.2 var array name =new Array (element 1, Element 2, Element 3 ...)

For example:
var arr2=new Array (5,6,7,8);

1.3 Assigned to an array by subscript, subscript starting from 0
var 数组名=new Array(数组长度);var 数组名[0]="数组第一个元素的值";var 数组名[1]="数组第二个元素的值";var 数组名[2]="数组第三个元素的值";
2. Property of the Array object Arr.join (Sep)

Divides all the elements in the array into a string using Sep , or commas as a delimiter if no separator is specified

Example 1:

var arr = new Array(3);arr[0] = "hello";arr[1] = "python";arr[2] = "javascript";document.write(arr.join());

Return:

hello,python,javascript

Example 2:

var arr = new Array(3);arr[0] = "hello";arr[1] = "python";arr[2] = "javascript";document.write(arr.join("."));

Return:

hello.python.javascript
Arr.concat (Array1,array2 ...)

The array1 can be a value or an array object that returns a new array that itself and the arguments are joined together

Example 1:

var a = [1,2,3];document.write(a.concat(4,5));

Return:

1,2,3,4,5

Example 2:

var arr = new Array(2);arr[0] = "hello";arr[1] = "python";var arr2 = new Array(2);arr2[0] = "hello";arr2[1] = "javascript";document.write(arr.concat(arr2));

Return:

hello,python,hello,javascript
Arr.reverse ()

The array arr is reversed and the original array is changed

Example:

var arr = new Array(3);arr[0] = "hello";arr[1] = "python";arr[2] = "javascript";document.write(arr + "<br />");document.write(arr.reverse());

Return:

hello,python,javascriptjavascript,python,hello
Arr.sort ()

Sorts the elements of the array, and the original arrays are changed

Example:

var arr = [11,33,55,77,66,44,22];document.write(arr.sort());

Return:

11,22,33,44,55,66,77
Arr.slice (Start,end)

Returns a new array of elements from start(including start) to end(not including end) from the array arr
When end is not specified, an array of all elements from start to end is returned

Example 1:

var arr = [11,33,55,77,66,44,22];document.write(arr.slice(2,6));

Return:

55,77,66,44

Example 2:

var arr = [11,33,55,77,66,44,22];document.write(arr.slice(2));

Return:

55,77,66,44,22
Arr.splice (start,deletecount,value1,value2)

Deletes an element of length deletecount from the start index of the array arr , and adds value1,value2to the location of the element where the array was deleted, The deleted element is then returned, and the original array is changed

Example:

var arr = [11,33,55,77,66,44,22];document.write(arr.splice(2,3,88,99)+"<br>");document.write(arr);

Return:

55,77,6611,33,88,99,44,22
Arr.push (VALUE1,VALUE2,VALUE3)

Adds one or more elements to the end of the array and returns the length of the new array

Example:

var arr = [11,33,55,77,66,44,22];document.write(arr.push(88,99)+"<br>");//返回数组的长度document.write(arr);    //返回新的数组

Return:

911,33,55,77,66,44,22,88,99
Arr.pop ()

Delete and return the last element of the array

Example:

var arr = [11,33,55,77,66,44,22];document.write(arr.pop()+"<br>");//删除并返回数组arr的最后一个元素document.write(arr);    //打印数组

Return:

2211,33,55,77,66,44
Arr.unshift (VALUE1,VALUE2,VALUE3)

Adds one or more elements to the beginning of the array and returns the length of the new array

Example:

var arr = [11,33,55,77,66,44,22];document.write(arr.unshift("aa","bb","cc")+"<br>");document.write(arr);

Return:

10aa,bb,cc,11,33,55,77,66,44,22
Arr.shift ()

Delete and return the first element of the array

Example:

var arr = [11,33,55,77,66,44,22];document.write(arr.shift()+"<br>");document.write(arr);

Return:

1133,55,77,66,44,22
3. Iterating the array

Use a For loop to traverse the entire array

Defining an array arr1

var arr1 = [12,23,34,"python","js"];

You can traverse the arr1 array in two ways with A For loop:

Mode 1:

for (i in arr1){    console.log(arr1[i]);}

Mode 2:

for(var i=0;i<arr1.length;i++){    console.log(arr1[i])}
4. Number of elements in the array, length property
console.log(arr1.length);

Array objects in JavaScript

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.