JavaScript - 内置函数删除对象中的多个键? [英] JavaScript - Built in Function to Delete Multiple Keys in an Object?

查看:58
本文介绍了JavaScript - 内置函数删除对象中的多个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

删除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];

推荐答案

这是一个类似于你要求的单行。

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}

这篇关于JavaScript - 内置函数删除对象中的多个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆