简体   繁体   English

从TypeScript对象中删除动态键

[英]Delete a dynamic key from a TypeScript object

In TypeScript it is quite simple to clone an object: 在TypeScript中,克隆对象非常简单:

const a = {...b}

or clone and update 或克隆并更新

const a = {...b, c: 'd'}

So for example, I have this code: 因此,例如,我有以下代码:

const a = {
    'something': 1,
    'e': 2,
};
const c = 'something';
delete a[c];

Is there a nice way to delete a property of that object, instead of using the traditional delete a[c] way? 有什么好方法可以删除该对象的属性,而不是使用传统的delete a[c]方法吗? (of course also not a[c] = undefined ) (当然也不a[c] = undefined

You're looking for combination of computed property names and destructuring. 您正在寻找计算的属性名称和解构的组合。 More info here 更多信息在这里

const a = {
    'something': 1,
    'e': 2,
};

const c = 'something';

const { [c]: _, ...withoutC } = a;

Here we're putting value of property something (taken from c variable) into _ variable and all the other props go to withoutC variable. 在这里我们把财产的东西 (取自值c变量)到_变量和所有其他的道具去withoutC变量。 The fact that c defined as const allows typescript to infer the type of withoutC properly. c定义为const的事实允许打字稿正确地推断出withoutC的类型。

Rather than deleting the property from a , use destructured assignment to create a new object without that property: 而不是删除财产a ,用解构后的分配,而不该属性创建一个新的对象:

const {c, ...b} = a;

After this b will contain all members of a except c . 此后b将包含ac之外的所有成员。

Given that a is some type, say, { c: string, d: string } the types of c and b will be inferred to be string and { d: string } respectively. 假设a是某种类型,例如{ c: string, d: string }则将cb的类型分别推断为string{ d: string } Of course, if you have to explicitly write these type annotations, using an Omit type as @Nurbol Alpybayev suggests is usually a lot better than having to spell out the types in long form. 当然,如果必须显式地编写这些类型注释,则使用@Nurbol Alpybayev建议的Omit类型通常比必须以长格式拼写出类型要好得多。

You can rename c to avoid conflicts with another name using this syntax: 您可以使用以下语法重命名c以避免与其他名称冲突:

const {c: someOtherName, ...b} = a;

The above method will work if you know the property name at compile time. 如果您在编译时知道属性名称,则上述方法将起作用。 If that's not the case in your scenario, then the TypeScript compiler can't really help you that much because it won't be able to determine the result type of the operation. 如果您的情况不是这种情况,那么TypeScript编译器实际上无法为您提供太大帮助,因为它无法确定操作的结果类型。

You'd be better off typing a as { [k: string]: number } , in which case delete a[c] would be fine, or you could use something like the following: 您最好输入a作为{ [k: string]: number } ,在这种情况下, delete a[c]就可以了,或者您可以使用类似以下的内容:

const exclude = <T>(map: { [k: string]: T }, omitted: string[]): { [k: string]: T } =>
  return Object.getOwnPropertyNames(a)
    .filter(k => omitted.indexOf(k) >= 0)
    .reduce((a, k) => (a[k] = map[k], a), {})
};
var b = exclude(a, ["c"]);

Sure! 当然! You can do this using Omit type: 您可以使用Omit类型执行此操作:

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>

type DeleteBar = Omit<{bar: 123, baz: 123}, 'bar'> // {baz: 123}

PS I realized that you might have asked about values, not types. PS我意识到您可能会问值,而不是类型。 You should have ask about javascript probably, not typescript. 您应该问关于javascript的问题,而不是打字稿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM