Classic characteristics of the array js

  • Creating arrays  
  1. var  arrayObj =  new new the Array (); // create an array  
  2. var  arrayObj =  new new the Array ([size]); // create an array and specified length, attention is not the upper limit, the length  
  3. var  arrayObj =  new new the Array ([element0 [, element1 [, ... [, elementN]]]]); create an array and assign  

    It is noted that, although the second method creates an array of specified length, but in fact an array of all cases are longer, which means that even if you specify a length of 5, can still be stored outside of the prescribed length of the element, Note: At this length will change.

  • Adds an array of elements
  1. . ArrayObj push ([item1 [item2 [[itemN]]]...]); // add one or more new elements to the end of the array, and returns the new length of the array 
  2. arrayObj.unshift ([item1 [item2 [[itemN]]]...]); // add one or more new elements to the array begins, the elements in the array automatic shift returns the new length of the array 
  3. arrayObj.splice (insertPos, 0, [item1 [, item2 [, [, itemN]]]...]); // one or more new elements into the array of the specified position, the insertion position of the element is automatically moved back ,return"".
  • Access the elements of an array  
  1. var  testGetArrValue = arrayObj [. 1]; // Get the value of the array element  
  2. arrayObj [1] = "This is the new value"; // new value given to the array elements
  • Delete array element
  1. arrayObj.pop (); // removes the last element and returns the element value  
  2. arrayObj.shift (); // remove a forwardmost element and returns the element values, elements in the array automatically forward  
  3. arrayObj.splice (deletePos, deleteCount); // delete the specified number deleteCount starting at the specified location deletePos elements, returns an array of elements removed
  • And merge array of interception
  1. arrayObj.slice (start, [end]); // return a portion of the array as an array of elements corresponding end note does not include all of the elements will be omitted if after the copy start end  
  2. arrayObj.concat ([item1 [, item2 [, [, itemN]]]...]); // plurality of arrays (or a string, array and string or mixed) is connected to an array, good new array return connection
  • A copy of the array
  1. arrayObj.slice (0); // returns a copy of an array of the array, a new array is noted, not directed  
  2. arrayObj.concat (); // returns a copy of an array of the array, a new array is noted, not directed
  • Sorting array elements
  1. arrayObj.reverse (); // reversing elements (most discharged before the end of the last routed foremost), returns an array of address  
  2. arrayObj.sort (); // sort the array elements, an array of return address 

Guess you like

Origin www.cnblogs.com/ltb6w/p/10991628.html