![]() |
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 KeywordIn 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.
Output:Updated Dictionary:
{
Java: 'Programming Language',
'C++': 'Programming Language',
TypeScript: 'Programming Language'
} Using Object DestructuringIn this approach, we are using object destructuring to remove keys specified in the keys array from the TypeScript dictionary dict. By extracting the properties associated with keys “Python” and “JavaScript” into separate variables (Python and JavaScript), and using the spread operator (…temp) to get the remaining properties into the temp object, we then assign temp back to the original dict and print the updated dict. Syntax:const { property1, property2, ... } = sourceObject; Example: The below example used Object Destructuring to remove keys from a TypeScript Dictionary.
Output:Updated Dictionary:
{
Java: 'Programming Language',
'C++': 'Programming Language',
TypeScript: 'Programming Language'
} Using Object.keys() and Array.reduce() methodsIn this approach, we are using Object.keys() and Array.reduce() methods to remove keys specified in the keys array, creating a new object that is then assigned back to the original TypeScript dictionary dict. Syntax:Object.keys(obj);
array.reduce(callback, initialValue); Example: The below example used the Object.keys() and Array.reduce() methods to remove keys from a TypeScript Dictionary.
Output:Updated Dictionary:
{
Java: 'Programming Language',
'C++': 'Programming Language',
TypeScript: 'Programming Language'
} Using Object.assign()Another method to remove keys from a TypeScript dictionary is by utilizing Object.assign(). This method creates a new object by merging properties from multiple objects, allowing us to exclude specific keys during the merging process. Syntax:Object.assign(target, ...sources); Example: In this example we creates a dictionary, removes specified keys, and stores the updated dictionary. It then logs the updated dictionary.
Output: {
"Java": "Programming Language",
"C++": "Programming Language",
"TypeScript": "Programming Language"
} |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |