Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Built in Function to Delete Multiple Keys in an Object?

Tags:

javascript

People also ask

How do I delete multiple object keys?

There is one simple fix using the library lodash. The _. omit function takes your object and an array of keys that you want to remove and returns a new object with all the properties of the original object except those mentioned in the array.

How do I delete key in on object JavaScript?

Use delete to Remove Object Keys The special JavaScript keyword delete is used to remove object keys (also called object properties). While you might think setting an object key equal to undefined would delete it, since undefined is the value that object keys that have not yet been set have, the key would still exist.

Is there a delete function in JavaScript?

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.


Here's a one-liner similar to what you're requesting.

var obj = {a: 1, b: 2, c: 3, d: 4, e: 5 };

['c', 'e'].forEach(e => delete obj[e]);

// obj is now {a:1, b:2, d:4}

You can also use Object Destructuring.

const obj = {
        a: 'dog',
        b: 'cat',
        c: 'mouse',
        d: 'cow',
        e: 'horse',
    };
const {a, e, ...updatedObject} = obj;

// output
console.log(updatedObject)

There is no built in js function to delete multiple keys yet, you can use any library for this such as underscore.js to do this.

_.omit({name: 'moe', age: 50, userid: 'moe1'}, 'userid');
=> {name: 'moe', age: 50}

//or you can use function to filter keys 

_.omit({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object){
  return _.isNumber(value);
});
=> {name: 'moe', userid: 'moe1'}

There is no built-in function, but one way you could do it is to add the multidelete method to the Object prototype. It's perhaps a little bit overkill, but you might find it useful.

if (!('multidelete' in Object.prototype)) {
    Object.defineProperty(Object.prototype, 'multidelete', {
        value: function () {
            for (var i = 0; i < arguments.length; i++) {
                delete this[arguments[i]];
            }
        }
    });
}

var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };

obj.multidelete('c', 'e'); // { a: 1, b: 2, d: 4 };

DEMO


There is one simple fix using the library lodash.

The _.omit function takes your object and an array of keys that you want to remove and returns a new object with all the properties of the original object except those mentioned in the array.

This is a neat way of removing keys as using this you get a new object and the original object remains untouched. This avoids the problem of mutation where if we removed the keys in the original object all the other parts of code using that object might have a tendency to break or introduce bugs in the code.

Example:

var obj = {x:1, y:2, z:3};
var result = _.omit(obj, ['x','y']);
console.log(result);

//Output
result = {z:3};

Link for the documentation of the same Click Here