Remove Object from Array using JavaScript

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

    ES2015

    let 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"}
                ];
    
    someArray = someArray.filter(person => person.name != 'John');
    

    It will remove John!

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

    I have made a dynamic function takes the objects Array, Key and value and returns the same array after removing the desired object:

    function removeFunction (myObjects,prop,valu)
            {
                 return myObjects.filter(function (val) {
                  return val[prop] !== valu;
              });
    
            }
    

    Full Example: DEMO

    var obj = {
                "results": [
                  {
                      "id": "460",
                      "name": "Widget 1",
                      "loc": "Shed"
                  }, {
                      "id": "461",
                      "name": "Widget 2",
                      "loc": "Kitchen"
                  }, {
                      "id": "462",
                      "name": "Widget 3",
                      "loc": "bath"
                  }
                ]
                };
    
    
            function removeFunction (myObjects,prop,valu)
            {
                 return myObjects.filter(function (val) {
                  return val[prop] !== valu;
              });
    
            }
    
    
    console.log(removeFunction(obj.results,"id","460"));
    
    0 讨论(0)
  • 2020-11-22 10:40

    Vote for the UndercoreJS for simple work with arrays.

    _.without() function helps to remove an element:

     _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
        => [2, 3, 4]
    
    0 讨论(0)
  • 2020-11-22 10:40

    If you want to remove all occurrences of a given object (based on some condition) then use the javascript splice method inside a for the loop.

    Since removing an object would affect the array length, make sure to decrement the counter one step, so that length check remains intact.

    var objArr=[{Name:"Alex", Age:62},
      {Name:"Robert", Age:18},
      {Name:"Prince", Age:28},
      {Name:"Cesar", Age:38},
      {Name:"Sam", Age:42},
      {Name:"David", Age:52}
    ];
    
    for(var i = 0;i < objArr.length; i ++)
    {
      if(objArr[i].Age > 20)
      {
        objArr.splice(i, 1);
        i--;  //re-adjust the counter.
      }
    }
    

    The above code snippet removes all objects with age greater than 20.

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

    Use splice function on arrays. Specify the position of the start element and the length of the subsequence you want to remove.

    someArray.splice(pos, 1);
    
    0 讨论(0)
  • 2020-11-22 10:43

    If you wanna access and remove object of an array simply you can try something like this.

    // inside some function
    
    let someArray = [ {"ColumnName" : "a", "PropertySerno" : 100005,"UpdateType" : 1},
                      {"ColumnName" : "b", "PropertySerno" : 100202,"UpdateType" : 1,
            "ShowRemoveButton" : true} ];
            
            for (let item of someArray) {
              delete item.ShowRemoveButton;
            }
            console.log(item.outputMappingData.Data);
            
    //output will be like that = [ {"ColumnName" : "a", "PropertySerno" : 100005,"UpdateType" : 1},
    //                             {"ColumnName" : "b", "PropertySerno" : 100202,"UpdateType" : 1 }];
            

    0 讨论(0)
提交回复
热议问题