Array creation, and methods of arrays

Source: Internet
Author: User

First, how to create an array?

There are three common ways to create an array:

The first type:

var arr = new Array (3); arr[0] = "Hello"; arr[1] = "world"; arr[2] = 2017;

The second type:

var arr2 = new Array ("Hello", "World", "2017");

The third type:

var arr3 = ["Hello", "World", 2017]

Second, some methods of operation of the array

1. The length property of the array: used to calculate the lengths of the array, that is, the number of array elements

var arr = [1,2,3,4,5];console.log (arr.length); The result is 5.

2. The push method of the array: used to add new elements to the end of the array

var arr = [1,2,3,4,5];arr.push (6,7,last); Console.log (arr)//output result = [1,2,3,4,5,6,7,last]

3. Unshift method for arrays: Used to add elements to the front of an array element

var arr = [1,2,3,4,5];arr.unshift (0,-1, "before"); Console.log (arr); The result is  [0,-1,before,1,2,3,4,5]

4. Array of pop methods: used to delete the last element inside the array

var arr = [1,2,3,4,5];arr.pop (); Console.log (arr); The result is [1,2,3,4]

5. The shift method of the array: used to delete the first element inside the array

var arr = [1,2,3,4,5];arr.shift (); Console.log (arr); The result is [2,3,4,5]

6. Array of splice methods: used to cut the elements of the group, and can be added and modified operation

var Namearr = ["Yangyang", "Fang Fang", "Round Round", "xiaoming"];//when splice has only one parameter n splice (n), only the first n elements of the array are preserved, followed by the deletion of Namearr.splice (3); Console.log ( Namearr); The result is ["Yangyang", "Fang Fang", "Round Round"]//when splice has two parameters splice (n,m), the first parameter n is the starting position of the deletion, the second parameter m represents the number of deletions, remember the position of the element in the array, n It is not the subscript namearr.splice in the array; Console.log (Namearr); The result is ["Yangyang", "Xiao Ming"]//when splice has multiple parameters splice (n,m,k), the first parameter n represents the starting position of the parameter, the second parameter m represents the number of deletions, where the third parameter K represents, The third and above elements are inserted into our deleted position namearr.splice (2,2,99,999); Console.log (Namearr); The result is ["Yangyang", "Fang Fang", 99,999]

6. Sorting methods for array elements: sort (from small to large), reverse (from large to small)

var Namearr = ["Tom", "Alex", "Julia", "Mike", "Frank"];namearr.sort (); Console.log (Namearr); The results are: ["Alex", "Frank", "Julia", "Mile", "Tom"];var nameArr2 = ["Tom", "Alex", "Julia", "Mike", "Frank"];namearr2.reverse (); Console.log (NAMEARR2); The results were: ["Frank", "Mike", "Julia", "Alex", "Tom"];//using sort and reverse input ordering, not in alphabetical order, but using ASCLL code, the U.S. Standard Information Interchange code to sort the// So how do you sort a set of numbers? There are two common sorting methods, one is the bubble sort method, the other is the sort method of the array: several years we only introduce the method of the array var numarr = [12,1,2,22,123,38];//from small to Numarr.sort (A, B ) {return a-B;}); Console.log (Numarr); The result is [1,2,12,22,38,123]//from large to small sort numarr.sort (function (A, b) {return b-a;}); Console.log (Numarr); The result is [123,38,22,12,2,1]

7. Join method for arrays: converting arrays to strings

var arr2 = ["Mike", "Mary", "Julia", "Tom"];var str = arr.join (","),//join the symbol in parentheses represents the symbol used to split the string, the default is comma Console.log (str);  The result is mike,mary,julia,tom//remember: the Jion () method does not operate in the original array, but instead returns a new one;

8. The slice method of the array: the same is the method of slicing the array, but it does not work in the original array, but instead produces a new array

var arr = ["Mike", "Mary", "Julia", "Tom"];//when there is only one argument in the slice, and the argument is positive, the parameter and its subsequent interception are all, and the parameter value is the subscript var newArr = Arr.slice (2) of the array; Console.log (NEWARR);//The result is: ["Julia", "Tom"];//when the only argument in slice is a negative number, the representation starts from the end of the array and retains the next few elements var newArr = Arr.slice (-3); Console.log (NEWARR);//The result is: ["Mary", "Julia", "Tom"];//when there are two parameters in slice, it represents the start and end position of the deletion, but the returned array has no end position in the element var newArr = Arr.slice; Console.log (NEWARR);//The result is: ["Mary"];

At present, the main commonly used in the array of methods, summed up these, if there are other methods, I will always give you to join in. Of course, you are welcome to share to me.

 

    

Creation of arrays, and methods of arrays

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.