Array api

First, the impact on the original array api

  1.push () is added at the end

  eg: source array: var arr = [5, 20, 6, 5, 80, 4]

    After using push (): arr.push (123); becomes: [5, 20, 6, 5, 80, 4,123]

  2.pop () remove the last
  eg: source array: var arr = [5, 20, 6, 5, 80, 4]
    After using pop (): arr.pop (); becomes: [5, 20, 6, 5, 80]
  3.unshift () added in front of
  eg: source array: var arr = [5, 20, 6, 5, 80, 4]
    After using unshift (): arr.unshift (123); becomes: [123,5, 20, 6, 5, 80,4]
  4.Shift () retrieves the first
   eg: source array: var arr = [5, 20, 6, 5, 80, 4]
    After using shift (): arr.shift (); becomes: [20, 6, 5, 80,4]
  5.sort () method for sorting an array
    Parameter is a callback function! ! !
    If there is no argument: it is sorted according to the first code of ascII
  eg: source array: var arr = [5, 20, 6, 5, 80, 4]
    Use sort ():    
               arr.sort(function (val1, val2) {
      // return val1-val2; // val1-val2 <0, in ascending order
      return val2 - val1; // val1-val2> 0, in descending order
    });
Second, the element group does not operate
  Splicing 1.concat array does not affect the original array
 
   EG was ARRL = [1,2,3,4];
      was Arr 2 = [11,12,13,14];
      var newArr = arrl.concat(arr2);
      console.log(newArr);
  
  Run Results: [1, 2, 3, 4, 11, 12, 13, 14]
 
  2.join ( "") will be divided into an array of strings, will not affect the original array of characters filled in accordance with
 
  EG was ARRL = [1,2,3,4];
     var arr2=arrl.join("-");
    console.log(arr2);
 
  The result: 1-2-3-4

  3.toString into a string array, the array elements does not change the original array separated by commas
 
  EG was ARRL = [1,2,3,4];
      var arr2= arrl.toString();
      console.log(arr2);
 
  The result: 1,2,3,4

  4.slice (start, end) in accordance with the standard array taken without including end does not affect the original array
 
  EG was ARRL = [1,2,3,4];
      console.log(arrl.slice(0,3));
 
 
  Run Results: [1, 2, 3]

  5.splice (start, deleteCount, arr) from start to start, delete a few arr newly inserted array elements of the original array operations
  EG was ARRL = [1,2,3,4];
      arrl.splice(1,2,78)
      console.log(arrl);
 
 
  Run Results: [1, 78, 4]
 
Third, examples
  
  1. Write function norepeat (arr) to remove elements of the array will be repeated
  
  was arr = [1, 2, 3, 5, 6, 7, 8, 9, 41, 2, 5, 2, 3, 65, 5, 2, 2, 1, 5, 5, 5, 25, 9, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 6, 5, 2, 5, 5, 5, 4, 41, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];

  function norepeat(arr) {
    while (true) {
      was flag = true;
      for (i = 0; i < arr.length; i++) {
        for (j = i + 1; j < arr.length; j++) {
          if (arr[i] == arr[j]) {
            arr.splice(j, 1);
            flag = false;
           }
        }
      }
    if (flag == true) {
      return arr;
     }
   }
 }
console.log(norepeat(arr));
 2. Quick Sort
was arr = [5, 2, 8, 4, 3, 6, 1, 7];
function quickSort(arr) {
  // recursive end condition
  if (arr.length <= 1) {
    return arr;
  }

// take intermediate values ​​and intermediate subscript
var midIndex = parseInt (arr.length / 2), // Why followed by a semicolon, the barrier will define an array, rather than a barrier of a variable defined midValue
    midValue = arr[midIndex],
    leftArr = [],
    rightArr = [];
    // iterate arr array, each of the intermediate value and a value compared to the right side of the large discharge array, the left array of small discharge
    for (var i = 0; i < arr.length; i++) {
      // If the subject is small middle, out of this cycle () without intermediate value and compare themselves
      if (i == midIndex) continue;
      if (arr[i] <= midValue) {
        leftArr.push(arr[i]);
      } else {
        rightArr.push(arr[i]);
      }
    }
    // recursive processing array around
    return quickSort(leftArr).concat(midValue).concat(quickSort(rightArr));
}
console.log(quickSort(arr));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/lxz123/p/11444133.html