On the array type of JS reference type

Source: Internet
Author: User

Recently read the Red Book, just tidy up and refine the knowledge inside!

How to create

There are two ways to create an array first.

    • One is: var arr = new Array ();

Arguments can be passed to the constructor, if only one argument is passed, and the parameter is a numeric value, then an array of that numeric size is created, and the other is creating an array containing the arguments

    • The other is: var arr = [];

Each item of an array can hold any type of data, var arr = [1, "Hello", true,{a:1}]; but it is not recommended to save different types of values

Length Property

1. Its value is always equal to the last item of the array index +1

  

var arr = [n/a];    arr[9] = ten;     Console.log (arr.length); // Ten

2. Use the Length property to achieve the effect of the push () function

  

var arr = [N/a      ]; = 4;      Console.log (arr); // [1, 2, 3, 4]      Arr.push (5);      Console.log (arr); // [1, 2, 3, 4, 5]

Detection method

You can use the instanceof operator and the IsArray () function (ES5)

Console.log ([] instanceof array);//trueconsole.log ({} instanceof array);//falseconsole.log (3 instanceof array);// Falseconsole.log (Array.isarray ([1]));//trueconsole.log (Array.isarray ({}));//false

Conversion method


All objects have tolocalstring (), toString (), and valueof () methods

For the ToString () method of an array, when the array calls this method, it is called on each item in the array to make them into strings, and then to stitch them together into a comma-separated string

Call valueof () or return the original array

Stack method and queue method

The push () and Pop () methods are LIFO (LIFO)

The shift () and Unshift () methods are FIFO

Reorder Methods

Reverse () and sort () methods

First, the simple reverse () method

var arr = [n/a]; var newArr = arr.reverse (); Console.log (arr); // [3, 2, 1]console.log (NEWARR); // [3, 2, 1]

It makes an array of items in reverse order, it modifies the original array, and the return value is also the modified array

The sort () method is cumbersome, by default, when no parameter is passed, the array is sorted in ascending order, the sort () method invokes the ToString () method of each array item, and then compares the resulting string to determine how to sort, Even if each item is a numeric value, it is converted to a string first.

var arr = [1,5,12,24,3,2];arr.sort (); Console.log (arr); // [1, 2, 3, 5]

1,12,2,24,3,5 This is what row size logic Ah, how not to give me in ascending order, this is because it compares the string, first from the first character, and then the second one ...

Thus, the method can be passed a comparison function, the rule is this: if the first parameter should be located before the second parameter will return a negative number, if the two parameters are equal, then return; Returns a positive number if the first parameter should be located after the second one.

var arr = [1,5,12,24,3,2]; function Compare (b) {    if(a > b) {        return 1;     Else if (A < b) {        return -1;     Else {        return 0;    }} Arr.sort (Compare); Console.log (arr); // [1, 2, 3, 5, .

This time, for the comparison is just the numeric type (note is the value is the premise) comparison function can also write

function Compare (b) {  return A- b;  }

This is a much simpler form!

Operation method

The Concat () method is used to stitch an array by creating a copy of the current array, then adding the accepted parameters to the end of the copy, and finally returning the newly constructed array

var arr = [+]; var arr2 = Arr.concat (3,4,[5,6]); Console.log (arr); // [1, 2]console.log (ARR2); // [1, 2, 3, 4, 5, 6]

The slice () method is used to intercept an array, accept one or two arguments, that is, the starting and ending positions, and when there is only one argument, slice () returns all items starting at the specified position of the parameter to the end of the current array. If it is

Two parameters, the array item is returned between the starting and ending positions (but excluding the end position);

vararr = [1,2,3,4,5,6];varARR2 = Arr.slice (0,1);varARR3 = Arr.slice (1);varARR4 = Arr.slice (2,5);varARR5 = Arr.slice (2,6);varARR6 = Arr.slice ( -4,-1);//equivalent to Arr.slice (2,5)varARR7 = Arr.slice (5,2); Console.log (arr);//[1, 2, 3, 4, 5, 6]Console.log (ARR2);//[1]Console.log (ARR3);//[2,3,4,5,6]Console.log (ARR4);//[3,4,5]Console.log (ARR5);//[3,4,5,6]Console.log (ARR6);//[3,4,5]Console.log (ARR7);//[]
You can see that items that do not include the end position, and that allow the parameters of the slice method to be negative, and if negative, to use the length of the array with the negative number to determine the corresponding position, and if the end position is less than the starting position, returns an empty array
Although the splice () method has only one letter with slice (), it implements a number of functions, it can achieve three functions of the array: 1. Delete, 2. Insert, 3. Replace
Delete: You need to specify two parameters: the position of the first item to delete and the number of items to delete
var arr = [1,2,3,4,5,6]; var arr2 = arr.splice (2,3); Console.log (arr);//[1,2,6]console.log (ARR2);//[3,4,5]
Insert: You can insert any number of items to the specified location, you need to provide 3 parameters: Start position, 0, the item to insert, and if you want to insert more than one item, you can pass in the fourth, fifth parameter
var arr = [1,2,3,4,5,6]; var arr2 = Arr.splice (2,0,7,8); Console.log (arr); // [1, 2, 7, 8, 3, 4, 5, 6]console.log (ARR2); // []
Replace: You can replace any item to a specified location, provide three parameters, start position, number of items to delete, and any number of items to insert, and the number of items inserted does not have to be equal to the number of items deleted.
var arr = [1,2,3,4,5,6]; var arr2 = Arr.splice (2,1,7,8); Console.log (arr); // [1, 2, 7, 8, 4, 5, 6]console.log (ARR2); // [3]
Splice always returns an array that contains items removed from the original array and returns an empty array if not deleted
Location method
The two new methods added in ES5: IndexOf () and LastIndexOf (), all accept two parameters: one is the item to find, and an optional index that represents the starting point of the lookup, the difference between the two methods is that the indexOf () is searched from the beginning of the array, LastIndexOf () is looked up from the end of the array, and they all return to find the position of the item in the array, return 1 if not found, and use the congruent operator in the lookup process, and he can only find one if there are multiple identical entries in the array to find, It can only find the first found item
var arr = [1,2,3,4,5,6,6,3,2];    Console.log (Arr.indexof (3)); // 2    Console.log (Arr.lastindexof (6)); // 6    Console.log (Arr.indexof (3,3)); // 7    Console.log (Arr.lastindexof (2,3)); // 1


Next introduction to iterative methods and merge methods




On the array type of JS reference type

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.