Skip to content
Advertisement

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

In JavaScript, I can delete an object’s key with

delete myObject[myKey];

Is there an efficient way to delete multiple keys using one line? Something that looks like:

multiDelete myObject[keyOne, keyTwo, keyThree];

Advertisement

Answer

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}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement