Array objects (Array)
An array of the most common properties: length Get the number of elements of the array
method:
toString () array into a string
var ARR = [ "Wuhan", "Chengdu", "in the city" ]; var before = String (ARR); // array into a string the console.log (before); // output Wuhan, Chengdu City, Changsha
concat () mosaic array, and returns an array of the stitched
was Arr1 = [1,2,3,4 ]; was arr2 = [888,999 ]; Arr1 = arr1.concat (56.78 , arr2); console.log (Arr1); // 1,2,3,4,56,78,888,999
join () array broken up into a character string delimited by the development of commonly used ""
var arr = [ "you", "Yes", "my", "best", "Friends" ]; arr = arr.join ( ""); // seamless console.log (arr); // output that you are my best friend
slice () selected array elements
// Select: Slice obtain an array element or a piece of a certain syntax: .slice array (starting index, the ending subscript + 1'd) var idcard = [4,2,0,4,3,4,1,8, 9,4,1,2,2,6,2,0,2,0 ]; var Print idcard.slice = (10,14 ); // Get birthday console.log (print);
splice () to delete an array element, add an array element, the array element replacement
// array of deletions, insertions, substitutions @ 1. Delete: Array .splice (subscript started, the number of deleted) var ARR = [1,2,3,4 ]; var arrdelete = arr.splice (0,1 ); // from 0 to delete a the console.log (ARR); // result is: [2,3,4] @ 2. insert: array .splice (start index, 0, insertion elements) var ARR = [11,22,33,44,55 ]; var arrinsert arr.splice = (0,0,00); // insert 000 will automatically parse the position 0 to 0 on the console.log (ARR); // results: [0,11,22,33,44,55] @ 3. replace: replace two steps -> delete, insert // array .splice (subscript started, the number of deleted, replaced elements ) var ARR = [78,88,98,3,4,5,6 ]; var= arr.splice arrreplace (0,3,0,1,2,10,14); // start the removal of 03 bits and begins at 0, 0,1,2 insert the console.log (ARR); // The results are: [0,1,2,3,4,5,6]
shift () Removes and returns the first element of the array
unshift () add an element at the beginning of the array
var ARR = [0,1,2 ]; arr.shift (); // delete the array element at the beginning of the console.log (ARR); // Output [1,2] arr.unshift (0); // at the beginning of the array a plurality of additive elements can be added to the console.log (ARR); // output [0,1,2]
pop () Removes and returns the last element of an array
push () is added to the end of the array elements
var bus = []; for( var i =1;i<=5;i++){ bus.push("乘客"+i); } console.log(String(bus)); while(bus.length>0){ var last= bus.pop(); console.log(last+"下车"); console.log(String(bus)); }
sort () to sort the array elements
var ARR = [1,5,3,6,4,2,20,14 ]; // Sort () using the default string in ascending order, if required collation has its own, the need to customize the comparator arr.sort (); the console.log (String (ARR)); // custom comparator function // comparator functions: compare any two specialized function of the magnitude values // 1. define a comparison function that takes two parameters a, b if a> b returns true (descending), otherwise returns false (ASC) function CMP (A, B) { return B- A; } arr.sort (CMP); the console.log (String (ARR));
reverse () element array reversal
// 2.Reverse () array element responsible reversed var ARR = [ 'A', 'B', 'C', 100 ]; arr.reverse (); the console.log (String (ARR)); // Output [ 100, 'c', 'b ', 'a']