How to use delete Keyword In Typescript
In this approach, we are using the delete keyword to remove keys specified in the keys array from the TypeScript dictionary dict. The forEach loop iterates through each key, deleting the associated property, resulting in an updated dictionary without the specified keys.
Syntax:
delete objectName.propertyName;
Example: The below example used the delete keyword to remove keys from a TypeScript Dictionary.
let dict: { [key: string]: string } = {
Java: "Programming Language",
Python: "Scripting Language",
"C++": "Programming Language",
JavaScript: "Scripting Language",
TypeScript: "Programming Language",
};
const keys: string[] = ["Python", "JavaScript"];
keys.forEach((key) => delete dict[key]);
console.log("Updated Dictionary:");
console.log(dict);
Output:
Updated Dictionary:
{
Java: 'Programming Language',
'C++': 'Programming Language',
TypeScript: 'Programming Language'
}
How to Remove Keys from a TypeScript Dictionary ?
In TypeScript, we can remove keys from a TypeScript Dictionary using various approaches that include deleting keywords, Object Destructuring, and by using Object.keys() and Array.reduce() methods.
There are several approaches to removing keys from a TypeScript Dictionary which are as follows:
Table of Content
- Using delete Keyword
- Using Object Destructuring
- Using Object.keys() and Array.reduce() methods
- Using Object.assign()
Contact Us