Delete multiple object properties?

后端 未结 9 967
天命终不由人
天命终不由人 2020-12-29 01:51

I create an object with multiple properties -

var objOpts = {
  option1: \'Option1\',
  option2: \'Option2\',
  option2: \'Option3\'
};

I t

相关标签:
9条回答
  • 2020-12-29 02:01

    One aditionnal otpion would be to use Object.assign to set the properties to either null or undefined depending on your use case. This does not delete the properties but clears them. So you would do either of these :

    // Modify the object directly
    Object.assign(objOpts, {option4: null, option5: null});
    

    OR

    // Create a copy of the object with the properties cleared 
    const newObject = Object.assign({}, objOpts, {option4: null, option5: null});
    

    You could also use an array to list the properties to clear with Array.reduce() to create the last param of Object.assign().

    0 讨论(0)
  • 2020-12-29 02:02

    One way is to create a separate function which takes your object and properties as argument.

    Js fiddle example

    Code also below:

    var objOpts = {
      option1: 'Option1',
      option2: 'Option2',
      option3: 'Option3',
      option4: 'Option4'
    };
    
    /** 
     * Method for removing object properties
     *
     */
    var removeObjectProperties = function(obj, props) {
    
        for(var i = 0; i < props.length; i++) {
            if(obj.hasOwnProperty(props[i])) {
                delete obj[props[i]];
            }
        }
    
    };
    
    // remove
    removeObjectProperties(objOpts, ["option1", "option2"]);
    
    // objOpts - after
    console.log(objOpts);
    
    0 讨论(0)
  • 2020-12-29 02:06

    I would also supply a more modern method than Mauno Vänä:

    function deleteProps (obj, prop) {
        for (const p of prop) {
            (p in obj) && (delete obj[p]);
        }    
    }
    

    Example:

    // Create sample object
    const myObject = {
        a: 'Foo',
        b: 'Baa',
        c: 'Oof'
    };
    
    // Prints: {a: "Foo", b: "Baa", c: "Oof"}
    console.log(myObject);
    
    // Delete props
    deleteProps(myObject, ['a', 'b']);
    
    // Prints: {c: "Oof"}
    console.log(myObject);
    
    0 讨论(0)
  • 2020-12-29 02:08
    var obj = {"key1":1,"key2":2,"key3":3,"key4":4};
    
    if (!('multidelete' in Object.prototype)) {
    Object.defineProperty(Object.prototype, 'multidelete', {
        value: function () {
            for (var i = 0; i < arguments.length; i++) {
                delete this[arguments[i]];
            }
        }
    });
    }
    
    obj.multidelete("key1","key3");
    

    You can use it like this to delete multiple keys in object

    0 讨论(0)
  • 2020-12-29 02:19

    ES6 provides an elegant solution to this: Rest in Object Destructuring:

    let { a, b, ...rest } = { a: 10, b: 20, c: 30, d: 40 };
    console.log(rest); // { c: 30, d: 40 }
    

    Note that this doesn't mutate the original object, but some folks might still find this useful.

    Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

    0 讨论(0)
  • 2020-12-29 02:22

    I'm sure you are trying to add custom properties to an object.

    A simpler way I would suggest is by creating a sub property:

    objOpts.custom.option4 = 'Option4'
    objOpts.custom.option5 = 'Option5'
    

    this way you could delete objOpts.custom and get done with it. Note that after this step you would have to recreate objOpts.custom = {}.

    Moreover this way would also feel closer to OOP, since your public properties would easily be distinguishable from private ones.

    If you are beginning with deleting objects in JavaScript, I'd like to point to to an excellently written article on the topic: http://perfectionkills.com/understanding-delete/

    You could play around with the meta properties which allow you to protect properties from being deleted etc. (to create an even better coding flow for your case)

    EDIT:

    I'd like to add that instead of deleting and recreating the property, you could simply say objOpts.custom = {} which would release the option4 and option5 from memory (eventually, via Garbage Collection).

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