javascript--Array

Source: Internet
Author: User

1. Arrays

The array type is a very common type. Although like arrays in other languages are an ordered list of data, arrays in ECMAScript are quite different. First, the array in the ECMAScript can hold any type of data (strings, numbers, objects, arrays, functions ...), Second, the array size in the ECMAScript can be dynamically adjusted. You can automatically grow as your data grows to accommodate new data two!

2. Creation of arrays

Literal quantity

var arr = [];   // declares an empty array var = [arr1];    // declares a 3-digit array with a length of 3 var arr2 = [4,5,6,,,,,]    // do not write like this! It's going to be bug!.

Array constructor

var New Array ();    // an empty array is created, equivalent to var arr3 = []; var New Array (5);     // An array of length 5 was created; var New Array ("1", "2", "3");    An array with 3 strings is created;

3. Methods of arrays

Arr.unshift (1, "123");//adds data at the beginning of the array.            Can be a hash value, if multiple are separated by commas; return value: Is the length of the new array. Arr.shift ();//Delete the first item of the array without writing the parameter; The return value is the value of the deleted itemArr.push ("1");//add data to the last item in the array, the parameter is the data you want to add, and the return value is the length of the new array.Arr.pop ();//Delete the last item of the array without writing the parameter, the return value is the value of the deleted item            //The array merge method, the parameter writes the value to be merged, can make the hash value, can also write the array literal directly; return value: the array after merging; The original array does not changeArr.concat ([8,9], 10);

//To intercept a segment in an array, the first parameter represents the index value to intercept the array start item, start, and the second parameter represents the index value to intercept the end of the array, the end; The argument can write a negative value to indicate the reciprocal (the last one represents-1)//can omit do not write end means intercept to the last (can be positive or negative) after interception results include start does not include End,start < end; return value: The array that was intercepted; The original array does not changeArr.slice (2,5); //The method for deleting, inserting, and replacing an array, the first two parameters must be written.//First parameter: index indicates the indexed value of the deleted item (or the index value of the inserted data)//the second parameter: Howmany indicates the number of deletions. If 0 means that no items are deleted, the insert operation is performed, and the following arguments: Represents the element to insert or replace. Can be omitted, indicating deletion//return value: Deletes an array of data, if no data is deleted to return an empty array. Arr.splice (index,howmany,1,2); //the array is reversed and sorted, the original array is changed, and the return value: The array after the flashback;Arr.reverse ()//reverse Order//sorting, which can be sorted by character encoding encoding without writing parameters. numeric uppercase lowercase letters; Each item in the array is first converted to a string and then sorted by character encoding, from small to large. Return value: Sorted arrayArr.sort ()
//Custom Descending sortvararr = [10000000,4,56,900,37,67,56];
varArrnew = Arr.sort (function(A, b) {if(A >b) {
return-1; }Else if(A = =b) { return0; }Else{ return2; }}); Console.log (arrnew); //converting an array to a string can be omitted, indicating that each item in the array is concatenated with a comma, converted to a string, or a hyphen that is to be stitched, representing the character to be stitched together. Return value: The string after the connection    vararr = [10000000,4,56,900,37,67,56]; varstring = Arr.join ();//10000000,4,56,900,37,67,56 default is comma split    varstr = Arr.join ("-");//10000000-4-56-900-37-67-56

javascript--Array

Related Article

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.