Remove array element based on object property

后端 未结 12 733
臣服心动
臣服心动 2020-11-22 08:19

I have an array of objects like so:

var myArray = [
    {field: \'id\', operator: \'eq\', value: id}, 
    {field: \'cStatus\', operator: \'eq\', value: cSta         


        
相关标签:
12条回答
  • 2020-11-22 09:12
    var myArray = [
        {field: 'id', operator: 'eq', value: id}, 
        {field: 'cStatus', operator: 'eq', value: cStatus}, 
        {field: 'money', operator: 'eq', value: money}
    ];
    console.log(myArray.length); //3
    myArray = $.grep(myArray, function(element, index){return element.field == "money"}, true);
    console.log(myArray.length); //2
    

    Element is an object in the array. 3rd parameter true means will return an array of elements which fails your function logic, false means will return an array of elements which fails your function logic.

    0 讨论(0)
  • 2020-11-22 09:17

    Say you want to remove the second object by it's field property.

    With ES6 it's as easy as this.

    myArray.splice(myArray.findIndex(item => item.field === "cStatus"), 1)
    
    0 讨论(0)
  • 2020-11-22 09:17

    Based on some comments above below is the code how to remove an object based on a key name and key value

     var items = [ 
      { "id": 3.1, "name": "test 3.1"}, 
      { "id": 22, "name": "test 3.1" }, 
      { "id": 23, "name": "changed test 23" } 
      ]
    
        function removeByKey(array, params){
          array.some(function(item, index) {
            return (array[index][params.key] === params.value) ? !!(array.splice(index, 1)) : false;
          });
          return array;
        }
    
        var removed = removeByKey(items, {
          key: 'id',
          value: 23
        });
    
        console.log(removed);
    
    0 讨论(0)
  • 2020-11-22 09:18

    Following is the code if you are not using jQuery. Demo

    var myArray = [
        {field: 'id', operator: 'eq', value: 'id'}, 
        {field: 'cStatus', operator: 'eq', value: 'cStatus'}, 
        {field: 'money', operator: 'eq', value: 'money'}
    ];
    
    alert(myArray.length);
    
    for(var i=0 ; i<myArray.length; i++)
    {
        if(myArray[i].value=='money')
            myArray.splice(i);
    }
    
    alert(myArray.length);
    

    You can also use underscore library which have lots of function.

    Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support

    0 讨论(0)
  • 2020-11-22 09:19

    jAndy's solution is probably best, but if you can't rely on filter you could do something like:

    var myArray = [
        {field: 'id', operator: 'eq', value: 'id'}, 
        {field: 'cStatus', operator: 'eq', value: 'cStatus'}, 
        {field: 'money', operator: 'eq', value: "money"}
    ];
    
    myArray.remove_key = function(key){
        var i = 0, 
            keyval = null;
        for( ; i < this.length; i++){
            if(this[i].field == key){
                keyval = this.splice(i, 1);
                break;
            }
        }
        return keyval;
    }
    
    0 讨论(0)
  • 2020-11-22 09:23

    Using lodash library it is simple as this

    _.remove(myArray , { field: 'money' });
    
    0 讨论(0)
提交回复
热议问题