JavaScript - 内置函数来删除对象中的多个键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32534602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:25:55  来源:igfitidea点击:

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

javascript

提问by James

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

在 JavaScript 中,我可以删除一个对象的键

delete myObject[myKey];

delete myObject[myKey];

Sometimes, I need to delete quite a few keys, but not all of the values in the parent key that encapsulate the keys I want to delete.

有时,我需要删除相当多的键,但不是父键中包含要删除的键的所有值。

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

有没有一种使用一行删除多个键的有效方法?看起来像的东西:

multiDelete myObject[keyOne, keyTwo, keyThree];

multiDelete myObject[keyOne, keyTwo, keyThree];

回答by James

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}

回答by roraima

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)

回答by Mohan Dere

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

还没有内置的 js 函数来删除多个键,您可以使用任何库underscore.js来执行此操作。

_.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'}

回答by Andy

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

没有内置函数,但您可以这样做的multidelete一种方法是将该方法添加到 Object 原型中。这可能有点矫枉过正,但您可能会发现它很有用。

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

演示

回答by Eksa

There is one simple fix using the library lodash.

使用库 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.

_.omit 函数接受您的对象和要删除的键数组,并返回一个新对象,该对象具有原始对象的所有属性,但数组中提到的属性除外。

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

相同文档的链接单击此处

回答by Iklan Hits

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}