Delete a dynamic key from a TypeScript object

typescript object remove by key
javascript dynamically delete object property
remove element from json object javascript
remove item from object javascript
remove a specific key from object
lodash remove property from object
js remove key from object
typescript remove fields from object

In TypeScript it is quite simple to clone an object:

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? (of course also not 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. The fact that c defined as const allows typescript to infer the type of withoutC properly.

How to remove a property from a JavaScript object, The semantically correct way to remove a property from an object is to const prop = 'color' const newCar = Object.keys(car).reduce((object,� delete expression. Where expression should evaluate to a property reference, e.g.: delete delete object['property'] Parameters object The name of an object, or an expression evaluating to an object. property The property to delete. Return value

Rather than deleting the property from a, use destructured assignment to create a new object without that property:

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

After this b will contain all members of a except c.

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. 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.

You can rename c to avoid conflicts with another name using this syntax:

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.

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:

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"]);

delete operator, The JavaScript delete operator removes a property from an object; if no When in strict mode, if delete is used on a direct reference to a variable, this structure with two separate arrays (one for the keys and the other for the� delete car ['brand'] delete car. brand delete newCar ['brand'] Setting a property to undefined If you need to perform this operation in a very optimized way, for example when you’re operating on a large number of objects in loops, another option is to set the property to undefined .

Sure! You can do this using Omit type:

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

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

P.S. I realized that you might have asked about values, not types. You should have ask about javascript probably, not typescript.

Rename object key in JavaScript, Method 1: Renaming the object by simple assignment of variables. After the assignment of variable or variables we will delete the old key,value pair and print the� TypeScript Type Template. Let’s say you created an object literal in JavaScript as − var person = { firstname:"Tom", lastname:"Hanks" }; In case you want to add some value to an object, JavaScript allows you to make the necessary modification. Suppose we need to add a function to the person object later this is the way you can do this.

Object.keys( ) In JavaScript, It creates a fresh new object(instance) Automobile() and assigns it to a variable. It sets the constructor property i.e “color” of the object to Automobile. Object.keys()� The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

How to Dynamically Access Object Property Using Variable in , Here are some more FAQ related to this topic: How to remove a property from a JavaScript object � How to get day, month and year from a date object in JavaScript� The object Type in TypeScript February 24, 2017. TypeScript 2.2 introduced a new type called object.It represents any non-primitive type. The following types are considered to be primitive types in JavaScript:

Remove a property from a nested object, recursively � GitHub, * Remove all specified keys from an object, no matter how deep they are. * The removal is done in place, so run it on a copy if you don't want to� In TypeScript, we support much the same types as you would expect in JavaScript, with a convenient enumeration type thrown in to help things along. Boolean # The most basic datatype is the simple true/false value, which JavaScript and TypeScript call a boolean value.

  • I think you're looking for const { [c]: _, ...withootC } = a;
  • ah, awesome. Could you add an answer and explain how it works, this is what I've looked for
  • Really close, but in my case, it is a bit more complicated. For example: const a = { 'something': 1, 'e': 2, }; const c = 'something'; const { 'something': undefined, ...b } = a; work, but const a = { 'something': 1, 'e': 2, }; const c = 'something'; const { c: undefined, ...b } = a; doesn't
  • so the point here is c is not a string 'c'. It is a variable const c = 'something'. Hope this time it is clear enough
  • @NguyenPhongThien I'm still not sure if I understand the problem. If the property that you want to delete is known at compile time, my method will work. If the property name isn't known at compile time, the result type of the expression cannot be determined (except for mapped types like { [k: string]: number }).
  • no, it is not a problem at all. I'm finding a nicer way to clone an object and delete a key from that, instead of const a = {...b}; delete a[c]; and sometime later b = {...a}. Because I don't want to mutate the original object for a while. But if it is not possible, it will still not be a big issue
  • @NguyenPhongThien I've added one more solution that's more idiomatic than const a = {...b}; delete a[c];.
  • it doesn't really work, could you please let me know how does it work in this case: const a = { 'something': 1, 'e': 2, }; const c = 'something'; delete a[c];