Remove Object from Array using JavaScript

前端 未结 29 2113
南笙
南笙 2020-11-22 10:24

How can I remove an object from an array? I wish to remove the object that includes name Kristian from someArray. For example:

som         


        
相关标签:
29条回答
  • 2020-11-22 10:57

    With ES 6 arrow function

    let someArray = [
                     {name:"Kristian", lines:"2,5,10"},
                     {name:"John", lines:"1,19,26,96"}
                    ];
    let arrayToRemove={name:"Kristian", lines:"2,5,10"};
    someArray=someArray.filter((e)=>e.name !=arrayToRemove.name && e.lines!= arrayToRemove.lines)
    
    0 讨论(0)
  • 2020-11-22 10:58

    You can use several methods to remove item(s) from an Array:

    //1
    someArray.shift(); // first element removed
    //2
    someArray = someArray.slice(1); // first element removed
    //3
    someArray.splice(0, 1); // first element removed
    //4
    someArray.pop(); // last element removed
    //5
    someArray = someArray.slice(0, a.length - 1); // last element removed
    //6
    someArray.length = someArray.length - 1; // last element removed
    

    If you want to remove element at position x, use:

    someArray.splice(x, 1);
    

    Or

    someArray = someArray.slice(0, x).concat(someArray.slice(-x));
    

    Reply to the comment of @chill182: you can remove one or more elements from an array using Array.filter, or Array.splice combined with Array.findIndex (see MDN), e.g.

    // non destructive filter > noJohn = John removed, but someArray will not change
    let someArray = getArray();
    let noJohn = someArray.filter( el => el.name !== "John" ); 
    log("non destructive filter > noJohn = ", format(noJohn));
    log(`**someArray.length ${someArray.length}`);
    
    // destructive filter/reassign John removed > someArray2 =
    let someArray2 = getArray();
    someArray2 = someArray2.filter( el => el.name !== "John" );
    log("", "destructive filter/reassign John removed > someArray2 =", 
      format(someArray2));
    log(`**someArray2.length ${someArray2.length}`);
    
    // destructive splice /w findIndex Brian remains > someArray3 =
    let someArray3 = getArray();
    someArray3.splice(someArray3.findIndex(v => v.name === "Kristian"), 1);
    someArray3.splice(someArray3.findIndex(v => v.name === "John"), 1);
    log("", "destructive splice /w findIndex Brian remains > someArray3 =", 
      format(someArray3));
    log(`**someArray3.length ${someArray3.length}`);
    
    // Note: if you're not sure about the contents of your array, 
    // you should check the results of findIndex first
    let someArray4 = getArray();
    const indx = someArray4.findIndex(v => v.name === "Michael");
    someArray4.splice(indx, indx >= 0 ? 1 : 0);
    log("", "check findIndex result first > someArray4 (nothing is removed) > ",
      format(someArray4));
    log(`**someArray4.length (should still be 3) ${someArray4.length}`);
    
    function format(obj) {
      return JSON.stringify(obj, null, " ");
    }
    
    function log(...txt) {
      document.querySelector("pre").textContent += `${txt.join("\n")}\n`
    }
    
    function getArray() {
      return [ {name: "Kristian", lines: "2,5,10"},
               {name: "John", lines: "1,19,26,96"},
               {name: "Brian", lines: "3,9,62,36"} ];
    }
    <pre>
    **Results**
    
    </pre>

    0 讨论(0)
  • 2020-11-22 11:00

    Returns only objects from the array whose property name is not "Kristian"

    var noKristianArray = $.grep(someArray, function (el) { return el.name!= "Kristian"; });
    


    Demo:

     var someArray = [
                    {name:"Kristian", lines:"2,5,10"},
                    {name:"John", lines:"1,19,26,96"},
                    {name:"Kristian", lines:"2,58,160"},
                    {name:"Felix", lines:"1,19,26,96"}
                    ];
    			 
    var noKristianArray = $.grep(someArray, function (el) { return el.name!= "Kristian"; });
    
    console.log(noKristianArray);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
  • 2020-11-22 11:01

    You could also use some:

    someArray = [{name:"Kristian", lines:"2,5,10"},
                 {name:"John", lines:"1,19,26,96"}];
    
    someArray.some(item => { 
        if(item.name === "Kristian") // Case sensitive, will only remove first instance
            someArray.splice(someArray.indexOf(item),1) 
    })
    
    0 讨论(0)
  • 2020-11-22 11:02

    This Concepts using Kendo Grid

    var grid = $("#addNewAllergies").data("kendoGrid");
    
    var selectedItem = SelectedCheckBoxList;
    
    for (var i = 0; i < selectedItem.length; i++) {
        if(selectedItem[i].boolKendoValue==true)
        {
            selectedItem.length= 0;
        }
    }
    
    0 讨论(0)
提交回复
热议问题