Remove Object from Array using JavaScript

前端 未结 29 2053
南笙
南笙 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:53

    Simplest solution would be to create a map that stores the indexes for each object by name, like this:

    //adding to array
    var newPerson = {name:"Kristian", lines:"2,5,10"}
    someMap[ newPerson.name ] = someArray.length;
    someArray.push( newPerson );
    
    //deleting from the array
    var index = someMap[ 'Kristian' ];
    someArray.splice( index, 1 );
    
    0 讨论(0)
  • 2020-11-22 10:54

    You could use array.filter().

    e.g.

            someArray = [{name:"Kristian", lines:"2,5,10"},
                         {name:"John", lines:"1,19,26,96"}];
    
            someArray = someArray.filter(function(returnableObjects){
                   return returnableObjects.name !== 'Kristian';
            });
    
            //someArray will now be = [{name:"John", lines:"1,19,26,96"}];
    

    Arrow functions:

    someArray = someArray.filter(x => x.name !== 'Kristian')
    
    0 讨论(0)
  • 2020-11-22 10:54

    There seems to be an error in your array syntax so assuming you mean an array as opposed to an object, Array.splice is your friend here:

    someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}];
    someArray.splice(1,1)
    
    0 讨论(0)
  • 2020-11-22 10:56

    This is what I use.

    Array.prototype.delete = function(pos){
        this[pos] = undefined;
        var len = this.length - 1;
        for(var a = pos;a < this.length - 1;a++){
          this[a] = this[a+1];
        }
        this.pop();
      }
    

    Then it is as simple as saying

    var myArray = [1,2,3,4,5,6,7,8,9];
    myArray.delete(3);
    

    Replace any number in place of three. After the expected output should be:

    console.log(myArray); //Expected output 1,2,3,5,6,7,8,9
    
    0 讨论(0)
  • 2020-11-22 10:57

    I recommend using lodash.js or sugar.js for common tasks like this:

    // lodash.js
    someArray = _.reject(someArray, function(el) { return el.Name === "Kristian"; });
    
    // sugar.js
    someArray.remove(function(el) { return el.Name === "Kristian"; });
    

    in most projects, having a set of helper methods that is provided by libraries like these is quite useful.

    0 讨论(0)
  • 2020-11-22 10:57

    Your "array" as shown is invalid JavaScript syntax. Curly brackets {} are for objects with property name/value pairs, but square brackets [] are for arrays - like so:

    someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}];
    

    In that case, you can use the .splice() method to remove an item. To remove the first item (index 0), say:

    someArray.splice(0,1);
    
    // someArray = [{name:"John", lines:"1,19,26,96"}];
    

    If you don't know the index but want to search through the array to find the item with name "Kristian" to remove you could to this:

    for (var i =0; i < someArray.length; i++)
       if (someArray[i].name === "Kristian") {
          someArray.splice(i,1);
          break;
       }
    

    EDIT: I just noticed your question is tagged with "jQuery", so you could try the $.grep() method:

    someArray = $.grep(someArray,
                       function(o,i) { return o.name === "Kristian"; },
                       true);
    
    0 讨论(0)
提交回复
热议问题