Array of Javascript learning arrays

Source: Internet
Author: User

Arrays play a very important role in JavaScript. Any control development cannot be separated from the use of arrays.

constructor function
New Array () new Array (size) new Array (element0element1elementn)

Parameters
size

The expected number of array elements. Returns an array of 1ength fields that will be set to the value of size.

element0, ... elementn

A list of arguments for two or more values. When you use these parameters to call the constructor array (), the elements of the newly created array are initialized to these values, and its length field is also set to the number of arguments.

return value

The newly created and initialized array. If you do not use parameters when calling the constructor array (), the returned array is empty and the length field is 0. When the constructor is called, only one numeric argument is passed to it, and the constructor returns an array with the specified number and element undefined. When array{is called with another parameter, the constructor initializes the array with the value specified by the parameter. When a constructor is called as a function, and the new operator is not used, its behavior is exactly the same as when it is called with the new operator.

Here's how he's doing.

1. array.concat ()

Array joins, as the name implies, are 2 or more array groups to synthesize an array.

eg

var v1 = [1, 2, 3];

var v2 = new Array ();
V2.push (4);

var v3 = new Array (5, ' e ', ' n ', ' d ');

var array = V1.concat (v2, v3);

Console.log (array);

Results:[12345 " n " ] /span>

2.array.join ()

Grammar
Array array. Join (separator)

Parameters
Separator

Used to delimit the character or string of an array element in the returned string, which is optional. If this argument is omitted, a comma is used as the delimiter.

return value

-A string that is generated by converting each element of an array into a string and then connecting the strings to insert a separator string between two elements.

Describe

The method join () converts each array element into a string and then joins the strings to insert the specified separator string between the two elements. Returns the generated string.

You can use the split () method of the string object to perform the opposite operation by dividing a string into an array element.

eg

var s = array.join ();
var S1 = array.join (":");
Console.info (s);
Console.info (S1);

Results:

1,2,3,4,5,e,n,d does not pass delimiters, by default separated by commas1:2:3:4:5:e:n:d3. array.length array length, this property does not say much, there is one thing to note:var array =new array ();array[100] = 100; an array of length equal to 101 is defined, and no other location is worth the default of undefined;array.length=10; The length of the array is equal to 10, followed by the removal. eg

Console.info (Array.Length); 8
ARRAY[100] = 100;
Console.info (Array.Length); 101
Array.Length = 10;
Console.info (Array.Length); 10
Array.Length = 100;
Console.info (Array.Length); 100

4.Array.pop ()

The array of pop and push can simulate the stack operation, advanced after the

Grammar

array. Pop ()

return value

The last element of the array.

Describe

The method pop () removes the last element of the array, minus 1 of its length, and returns the value of the element it deleted. If the array is already empty, then pop () does not change the array and returns undefined.

5.Array.push ()

Grammar
array. Push (value, ...)

Parameters
value, ...

The value to add to the tail of the array, which can be one or more.

return value

Adds the specified value to the new length after the array.

Describe

The method push () adds its arguments sequentially to the end of the array. Instead of creating a new array, it modifies the array directly. Method push () and Method Pop () provide an advanced post-stack function with arrays

6.array.reverse ()

Reverses the order of the elements in the array

7.Array.shift ()

Grammar
array. Shift ()

return value

The original first element of the array.

Describe

The shift () method shifts the first element of the array to a group, returns the value of that element, and moves the remaining elements forward one bit to fill the empty array header. If the array is empty, shift () will not take any action, returning undefined. Note that the method does not create a new array, but instead modifies the original array directly.

Method Shift () is similar to Method Array.pop () except that it operates at the head of the array, not at the tail. This method is often used in conjunction with Unshift ().

8.array.unshift ()

Grammar
array. Unshift (value

Parameters
value, ...

One or more values to insert into the head of the array.

return value

The new length of the array

Describe

Method Unshift () inserts its arguments into the head of the array and moves the existing elements sequentially to a higher subscript so that space can be set aside. The first parameter of the method becomes the new element of the array 0, and if there is a second argument, it becomes the new element 1, and so on. Note that unshift () does not create a new array, but modifies the original array directly.

Example

The method Unshift () is usually used in conjunction with the method Shift (). For example:

var a = [];             A:[]a.unshift (1);           A:[1]          return to 1a.unshift (a);          a:[22,1]       return 2a.shift (  );            A:[1]          return 22a.unshift (33,[4,5]);    

9.Array.slice ()
Grammar
array. Slice (startend)
Parameters
Start

The array subscript at the beginning of the array fragment. If it is a negative number, it declares the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.

End

An array subscript for the last element at the end of the array fragment. If not specified, this parameter contains all elements from start to end of the array. If this parameter is a negative number, the element starting from the end of the array is counted.

return value

A new array that contains the array elements specified from start to end (excluding the element).

Describe

The method Slice () returns a portion of the array, or a sub-array. The returned array contains all the elements from start to end, but does not include the element that the end refers to. If end is not specified, the returned array contains all elements from start to the end of the original array.

Note: This method does not modify the array. If you want to delete an element from an array, you should use method Array.splice.

Example
var a = [1,2,3,4,5];a.slice (0,3);    return [1,2,3]a.slice (3);      return [4,5]a.slice (1,-1);   return [2,3,4]a.slice ( -3,-2);  return [3]; Bug in IE 4: Return to [+/-]

Array.Sort ()
Grammar
Array array. Sort (orderfunc)
Parameters
Orderfunc

The function to specify the order in which to sort, optional.

return value

A reference to an array. Note that the array is sorted on the original array without making a copy.

Describe

The method sort () sorts the array elements on the original arrays, i.e. does not create a new array copy when sorting. If no parameters are used when calling the method sort (), the elements in the array are sorted alphabetically (more precisely, in the order of character encodings). To do this, you should first convert the elements of the array to a string (if necessary) for comparison.

If you want to sort in a different order, you must provide a comparison function that compares two values and then returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:

    • If a is less than B according to your criteria, a value that is less than 0 is returned in the sorted array before a should appear before B.

    • If a equals B, it returns 0.

    • If a is greater than B, a value greater than 0 is returned.

Notice that the undefined elements in the array are arranged at the end of the array. This is true even if you provide a custom sort function, because the undefined value is not passed to the orderfunc you provide.

Example

The following code shows how to write a comparison function in numerical order instead of sorting an array alphabetically:

Sort functions sorted in numeric order function Numberorder (A, b) {return a-B;} A = new Array (4, 1111, 222); A.sort (  );             In alphabetical order the result is: 1111, 222, 4a.sort (numberorder);    Sort results in numerical order: 4, 33, 222, 1111

Array of Javascript learning 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.