zoukankan      html  css  js  c++  java
  • Javacript Remove Elements from Array

    參考自: https://love2dev.com/blog/javascript-remove-from-array/

    1. Removing Elements from End of Array

    var ar = [1, 2, 3, 4, 5, 6];
    ar.length = 4; // set length to remove elements
    console.log( ar ); //  [1, 2, 3, 4]
    
    
    var ar = [1, 2, 3, 4, 5, 6];
    ar.pop(); // returns 6
    console.log( ar ); // [1, 2, 3, 4, 5]

    2. Removing Elements from Beginning of Array

    var ar = ['zero', 'one', 'two', 'three'];
    ar.shift(); // returns "zero"
    console.log( ar ); // ["one", "two", "three"]

    3. Using Splice to Remove Array Elements

    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    var removed = arr.splice(2,2);
    
    /*
    removed === [3, 4]
    arr === [1, 2, 5, 6, 7, 8, 9, 0]
    */
    ["bar", "baz", "foo", "qux"]
    
    list.splice(0, 2) 
    // Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"].

    4. Removing Array Items By Value Using Splice

    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    
    for( var i = 0; i < arr.length; i++){ 
       if ( arr[i] === 5) {
         arr.splice(i, 1); 
       }
    }
    
    //=> [1, 2, 3, 4, 6, 7, 8, 9, 0]

    5. Using the Array filter Method to Remove Items By Value

    var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    
    var filtered = array.filter(function(value, index, arr){
    
        return value > 5;
    
    });
    
    //filtered => [6, 7, 8, 9]
    //array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

    6. The Lodash Array Remove Method

    var array = [1, 2, 3, 4];
    var evens = _.remove(array, function(n) {
      return n % 2 === 0;
    });
    
    console.log(array);
    // => [1, 3]
    
    console.log(evens);
    // => [2, 4]

    7. Making a Remove Method

    function arrayRemove(arr, value) {
    
       return arr.filter(function(ele){
           return ele != value;
       });
    
    }
    
    var result = arrayRemove(array, 6);
    
    // result = [1, 2, 3, 4, 5, 7, 8, 9, 0]

    8. Explicitly Remove Array Elements Using the Delete Operator

    var ar = [1, 2, 3, 4, 5, 6];
    delete ar[4]; // delete element with index 4
    console.log( ar ); // [1, 2, 3, 4, undefined, 6]
    alert( ar ); // 1,2,3,4,,6

    9. Clear or Reset a JavaScript Array

    var ar = [1, 2, 3, 4, 5, 6];
    //do stuff
    ar = [];
    //a new, empty array!
    
    
    var arr1 = [1, 2, 3, 4, 5, 6];
    var arr2 = arr1;  // Reference arr1 by another variable 
    arr1 = [];
    console.log(arr2); // Output [1, 2, 3, 4, 5, 6]
    
    
    var ar = [1, 2, 3, 4, 5, 6];
    console.log(ar); // Output [1, 2, 3, 4, 5, 6]
    ar.length = 0;
    console.log(ar); // Output []
    
    
    var ar = [1, 2, 3, 4, 5, 6];
    console.log(ar); // Output [1, 2, 3, 4, 5, 6]
    ar.splice(0, ar.length);
    console.log(ar); // Output []
    
    
    var ar = [1, 2, 3, 4, 5, 6];
    console.log(ar); // Output [1, 2, 3, 4, 5, 6]
      while (ar.length) {
        ar.pop();
      }
    console.log(ar); // Output []
  • 相关阅读:
    POJ1125 Stockbroker Grapevine(spfa枚举)
    20160402javaweb 开发模式
    20160331javaweb之JSP 标签技术
    20160331javaweb之JSP include 指令&&九大隐式对象
    20160330javaweb之session 小练习
    20160329javaweb之JSP -session入门
    20160329javaweb之JSP -cookie入门
    20160328 javaweb Cookie 小练习
    20160327javaweb 之JSP入门
    HttpServlet was not found on the Java
  • 原文地址:https://www.cnblogs.com/sipher/p/11563975.html
Copyright © 2011-2022 走看看