Remove array element based on object property

后端 未结 12 738
臣服心动
臣服心动 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: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;
    }
    

提交回复
热议问题