JavaScript Basics (ii) arrays

Source: Internet
Author: User
Tags sorts

String, the JavaScript string is represented by a character enclosed in "and".
character literal, \ n newline, \ t tab, \b Backspace, \ r Enter, \f, \ \ Slash, \ ' single quote ('), \ "Odd Number (")
\xnn a character in hexadecimal code nn (where n is 0~f) \unnnn A Unicode character represented by the hexadecimal code nnnn.
Character of the string, once created can not be changed, to change a variable to save the string, first to destroy the original string, and then with another containing the character
String to populate the variable.
Common operations:

var s = ' Hello world! ';
S.length;
To get a character at a specified position in a string, use an array-like subscript operation with the index number starting at 0.
var s = ' Hello world! ';
S[0]; H
s[6]//"
S[7]//' W '
s[12]//' l '
s[13]//' undefined out of range index does not error, but returns undefined '
It is important to note that the string is immutable, and there is no error if you assign a value to an index of the string, but it does not have any effect:
var s = ' Test ';
S[0] = ' X ';
alert (s); S is still ' Test '
      toUpperCase
toUpperCase () turns a string all uppercase "
var s= ' Hello '
S.touppercase (); Return ' HELLO '
      toLowerCase
toLowerCase () turns a string all lowercase:
var s = ' Hello ';
var lower = S.tolowercase (); Returns ' Hello ' and assigns a value to the variable lower
Lower ' Hello '
      indexOf
IndexOf () searches for the location where the specified string appears:
var s = ' Hello, world ';
S.indexof (' World '); Returns 7
S.indexof (' World '); The specified substring was not found, returned-1
     substring
SUBSTRING () returns the substring of the specified index interval:
var s = ' Hello, world '
S.substring (0, 5); Starting from index 0 to 5 (excluding 5), return ' Hello '
S.substring (7); Starting at index 7 to the end, return ' world '

An array of

array
JavaScript can contain any data type and access each element through an index.      
to get the length of the array, directly access the Long property:

var arr = [1, 2, 3.14, ' Hello ', NULL, true];
Arr.length;      6

Note that assigning a new value directly to the length of the array results in a change in the array size:
var arr = [All-in-one];
Arr.length;      3
Arr;//arr becomes [undefined,undefined,undefined]
Arr.length = 2;
Arr    Arr changes to [+]
Array can modify the corresponding element to a new value by index, so assigning a value to an array's index modifies the array directly:
var arr = [' A ', ' B ', ' C '];
ARR[1] = 99;    
arr;//arr now changes to [' A ', A, ' C ']
Note: If an index is assigned a value, the index is larger than the range, which also causes an array size change:
var arr = [1, 2, 3];
ARR[5] = ' x ';
arr;//arr becomes [1, 2, 3, Undefined, undefined, ' x ']

indexOf () and LastIndexOf ()
All two methods receive two parameters, the item to find and the index that represents the location of the lookup start, where the IndexOf method looks backwards from the beginning of the array
The LastIndexOf method starts looking forward from the end of the array. Two methods return-1 without finding it, requiring that the look must be strictly equal, and the
is like using (= = =).      The
array can also search for the location of a specified element through indexof ():
var arr = [10,20, ' + ', ' xyz '];
Arr.indexof (10); The index of element 10 is 0
Arr.indexof (20);//Index of element 20 is 1
Arr.indexof (30);//Element 30 not found, return-1
arr.indexof (' 30 ' ); The index of element ' 30 ' is 2
Note: The number 30 and the string ' 30 ' are different elements.

The

Slice
Slice () is the substring () version of the corresponding string that intercepts some elements of the array and returns a new array:
var arr = [' A ', ' B ' , ' C ', ' D ', ' E ', ' F ', ' G '];
Arr.slice (0,3);//starting at index 0, ending with index 3, but excluding index 3: [' A ', ' B ', ' C ']
Arr.slice (3);//starting from index 3 to end: [' D ', ' E ', ' F ', ' G ']
Note that the start and end parameters of slice () include the starting index, excluding the ending index.
If you do not pass any arguments to slice (), it will intercept all elements from beginning to end.      With this, we can easily copy an array:
var arr = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '];
var acopy = Arr.slice ();
Acopy;//[' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']
acopy = = = arr;//False

Push and pop
Push () adds several elements to the end of the array, and POPs removes the last element of the array.
Push: Returns the length of the modified array, pop (): Returns the item that was removed.
var arr =[1,2];
Arr.push (' A ', ' B ')//return array new length: 4
Arr://[1,2, ' A ', ' B ']
Arr.pop (); Pop () returns ' B '
Arr [Up, ' A ']
Arr.pop (): Arr.pop (); Arr.pop (); continuous pop 3 times
Arr // []
Arr.pop ();//empty array continue pop does not error, but returns undefined
Arr //[]

Unshift and Shift
If you want to add several elements to the head of an array, use the Unshift () method and the Shift () method to remove the first element of the array.
var arr = [+];
Arr.unshift (' A ', ' B '); Returns the array new length 4
ARR://[' A ', ' B ', up]
Arr.shift (); A
Arr [' B ', up]
Arr.shift (); Arr.shift (); Arr.shit (); Continuous Shift 3 times
Arr // []
Arr.shift (); Empty array continuous shift does not error, but returns undefined
arr;//[]

The

sort
Sort () sorts the current array directly, modifies the position of the elements of the current array, and, when called directly, sorts in the default order:
compares strings.
var arr = [' B ', ' C ', ' A '];
Arr.sort ();
arr;//[' A ', ' B ', ' C ']
from small to large: (ascending)
function Compare (value,value) {
if (value1 < value2 ) {
return-1;        
}else if (value1 >value2) {
return 1;
        }else{
return 0;
    }
}
var values = [0,1,5,10,15];
Values.sort (Compare);
Alert (values)//0,1,5,10,15
from large to small arrange (descending)
function Compare (value1,value2) {
if (value1 < value        2) {
return 1;
      }else if (value1 > value2) {
return-1;
      }else{
return 0;
    }
}
var values = [0,1,5,10,15];
Values.sort (Compare);
Alert (values);//15,10,5,1,0

The

Reverse
Reverse () removes all elements of the array and reverses them.
var arr = [' One ', ' one ', ' three '];
Arr.reverse ();
Arr;//[' three ', ' one ', ' one ']

The

Splice
Splice () method is a universal way to modify an array, with the primary purpose of inserting items into the middle of the array.
Delete: You can delete any number of items, just specify 2 parameters.
For example: Splice (0,2) deletes the first two entries in the array
Insert: You can insert any number of items to the specified location, just provide 3 parameters, in fact, 0 (the number of items to delete) and the item to insert, if
Insert multiple items, you can pass in the Four, five, multiple. For example: Splice (2,0, "Red", "green")
Replace: You can insert any number of items to the specified location. Delete any number of items at the same time, just specify 3 items, starting position, number of items to delete,
inserted items are not necessary and deleted items equal, splice (2,1, "Red", "green"), delete the entry of array position 2, and then insert string "R" from 2 position
Ed "and green.
var arr = [' Microsoft ', ' Apple ', ' Yahoo ', ' AOL ', ' Excite ', ' Oracle '];
//Delete 3 elements starting at index 2 and then add two elements:
Arr.splice (2, 3, ' Google ', ' Facebook ');//return deleted elements [' Yahoo ', ' AOL ', ' EXC Ite ']
arr;//[' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']
//delete only, do not add:
Arr.splice (2 , 2); [' Google ', ' Facebook ']
arr;//[' Microsoft ', ' Apple ', ' Oracle ']
//Add only, do not delete:
Arr.splice (2, 0, ' Google ', ' Facebook '); return [] because no element was removed
arr;//[' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']

Concat
The Concat () method joins the current array with another array and returns a new array:
var arr = [' A ', ' B ', ' C '];
var added = Arr.concat ([1, 2, 3]);
Added [' A ', ' B ', ' C ', 1, 2, 3]
Arr [' A ', ' B ', ' C ']
Note: the concat () method does not modify the current array, but instead returns a new array.
In fact, the concat () method can receive any element and array, and automatically takes the array apart and adds it all to the new array:
var arr = [' A ', ' B ', ' C ', 1, 2, 3];
Arr.concat (1, 2, [3, 4]); [' A ', ' B ', ' C ', 1, 2, 3, 4]

Join
The join () method is a useful way to concatenate each element of the current array with a specified string and then return the concatenated string:
var arr = [' A ', ' B ', ' C ', 1, 2, 3];
Arr.join ('-'); ' A-b-c-1-2-3 '
If the element of the array is not a string, it is automatically converted to a string and then concatenated.

Multidimensional arrays
If an element of an array is an array, a multidimensional array can be formed, for example:
var arr = [[1, 2, 3], [400, 500, 600], '-'];

JavaScript Basics (ii) arrays

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.