Getting Started those things array of JavaScript

The concept of an array: the referenced object type.

  1. Essence: a plurality of data stored in memory space, and then a name.
  2. Data structures: different data structures, different good operation.
  3. An array of features: easy to find and maintain data.

Creating an array:

  1. Method 1: var array element name = [1, element 2, element 3 ...... n] element;
  2. Note: You can create an empty array, it is recommended to create an assignment at the same time!
  3. Mode 2: var array name = new Array ();

An array of Use:

  1. Index: Each element has a room number for the default starting from 0, incremented by one.
  2. Element: in fact, saved in a data array, can be a variety of data types.
  3. Have access to an array of elements: the use of elements in the array and ordinary variables exactly the same.
  4. Use array name [subscript]: Gets the underlying data in the correspondence.
  5. Using the array name [index] = new value: the new value replaces the original value.

An array of attributes:

  1. .length attribute: record the number of array elements theoretically. The actual saving is the last element at +1 standard array. The number and the actual length of the element is not necessarily the same.
  2. Note: length from the array can not be used alone, do not need to manually modify automatically maintained.

Common operations:

  1. Get the last element in the array: arr [arr.length-1];
  2. Get the penultimate element of the n-th elements: arr [arr.length-n];
  3. At the end of the array pursue a new element:. Arr [] = arr.length new element;
  4. Modify the array's length property, you can achieve the purpose of deleting elements!
  5. Removes the last element in the array: arr.length--;
  6. Delete the last n elements of the array: arr.length- = n;
  7. About the nature of the array: type of reference object
  8. Output: dir: structure and contents of the output data, rather than immediately output, click on the small triangle after the output of the right content in memory!
  9. Traversal: each element of the array in order to find, and do the same!
      for (var i=0;i<=arr.length;i++) {
        arr[i];
      }  

Array Category:

  1. Indexed array: array subscript numbers
  2. Associative array: Custom index value
  3. Create an array: var arr = []; // create an empty array; arr [ "subscript name"]; = value; add a new element to an empty array, and a custom index names.
  4. Using arrays: general arrays as elements, use the same general array. Value: arr [index name] assignment: arr [index] = Name the new value;
  5. Note: length attribute associative array failure, always 0;
  6. Traversal: for in looping through an associative array!  
      for(var key in arr) {
        arr[key];
      }  

Notes: key: for each cycle to save the underlying variables under temporary variables; in: Keywords; arr: array name needs to traverse an array

   7. Summary: an array of features: easy to maintain and search data!

   8. associative array vs indexed array: associative array: Find a faster and more efficient. The reason: direct positioning element, the search speed is not the number of elements influence; indexed array: relatively slow! The reason: Only traversal search, search efficiency by the number of elements, the position of influence!

API array

  1. API: others have written, the program we used directly.
  2. Array: storing a plurality of data, and the operation of these API objects to provide data.
  3. String into an array

3.1 string(arr);  arr.toString();

3.2 API can be more than two indexed array to a string. ------ each element in the array and then converted to string between "," stitching.

3.3 arr.join ( "splice symbol"); each element in the array of the specified character spliced ​​splicing. Note: If the stitching is omitted, default use "," mosaic.

3.4 fixed routine:

var str="<ANY>"+arr.join("</ANY><ANY>")+"</ANY>"
            elem.innerHTML=str;

4. Select the array splice

  4.1 splicing: splice multiple elements or array to a new array;

 var new_arr=old_arr.concat(值,数组);

  Note: concat (); return new array, without modifying the original array; if required stitching is an array, the array elements to break one element in the mosaic!

  4.2 Select: selection element between the positions specified in the array, a new array returned composition.

  var new_arr=old_arr.slice(starti,endi);

  Note: Do not modify the original array

  If an API parameters are two subscripts, typically containing free end of the head!

slice(starti,starti+n);//n表示选取元素的个数

   Support negative argument: the principle of using the length property.

arr.slice(starti,-n)<=>arr.slice(starti,arr.length-n);

Not all API supports negative parameters, use arr.length-n.

The second parameter is omitted: Select from the starting position up to the end.

var new_arr=arr.slice();

  While omitting two parameters: copy the array;

var new_arr=arr.slice();

CRUD ---- splice 5. array

Increased 5.1: arr.splice (starti, 0, the value 1, value 2, value ...... value n-3);

  Note: The new value is inserted after the specified position, all subsequent shift positions that order; if the value of the array is inserted, not broken, the specified position is inserted between the array.

  5.2 Delete:

arr.splice(starti,n);  //从starti位置开始删除n个元素

  Note: directly modify the original array; support a negative argument; you can omit the second argument ---- has been deleted to the end; there is new data ---- return the deleted elements of value.

 5.3 Review:

arr.splice(starti,n,值1,值2.....);  其实就是将原值删除,加入新值!  //从starti位置开始,删除n个元素,在加入新值

  Note: Deleting elements need not be consistent with the preservation number of elements inserted!

6. Sort:

 6.1 Flip ---- Reverse:

  arr.reverse();  //将元素组中的元素按原顺序颠倒!

    Note: directly modify the original array, not return the new value; can not modify the content of the order, but the head and tail upside down!

   Sequential ordering 6.2: arr.sort (comparator function); // according to the contents of the array to a small large / small to large by sequential ordering!

   Note: The default parameter is not passed, the data in the array in ascending order; directly modifying the original array, not return the new value!

    Principle: sort by default elements in the array into a string, and then follow the character encoding compare the size!

   6.3 comparator functions: one by one comparison between the size of two elements.

format:

function comp(a,b){
      return a-b;
              }

     If a> b, need to return a number greater than 0!

     If a = b, the need to return a number equal to 0!

     If a <b, a need to return the number of less than 0!

7. Stack: open end, a closed end array structure!

7.1 stack, the stack:

  Stack: remove an element from the array.

  Stack: add an element to the array.

  Data structure with both ends open: 7.2 queue

8. The two-digit groups:

8.1

      eg:var arr=[[1,2,3,4],[3,4,5,6],[6,7,8,9]];

   8.2 traversing two-dimensional array: two steps: 1, to obtain a two-dimensional array of each sub-array; 2, traversing each subarray!

 

JavaScript some of the information

- JS knowledge Summary -

http://www.makeru.com.cn/live/1396_592.html?s=45051

- Into the JavaScript function in the world -

http://www.makeru.com.cn/live/1396_1016.html?s=45051

Guess you like

Origin www.cnblogs.com/QianD/p/11264176.html