reactjs - 如何根据键从 Typescript 中的 Dictionary 数据结构中删除一个条目?
问题描述
我正在使用字典数据结构,其中 Key 是 Id,值是 PersonInformation,
interface PersonInformation{
FirstName:string;
SecondName:string;
Age:string;
}
如何根据 ID 从字典中删除一个人的条目。
解决方案
如果你的字典是一个,object
你可以像这样删除一个对象属性:
// in case your dictionary is object:
const Dictionary = {
firstname: 'John',
lastname: 'Doe'
}
delete Dictionary['firstname']; // <-- Use your ID instead
console.log(Dictionary.firstname);
// expected output: undefined
// As in your comments you said your dictionary is like following:
//in case your dictionary is an Array
const Dictionary = [ ];
Dictionary.push({ key: PersonId , value: PersonDescription })
//in this case you can do this:
const newDictionary = Dictionary.filter(item => item.key !== id)
// newDictionary is an Array without the item with key === id
在操场上查看此链接
推荐阅读
- node.js - NodeJS 如何使用 crypto-js 或使用自定义密钥大小、iv、模式和填充以 UTF8 编码的加密包加密(AES 算法)数据
- javascript - 为什么我打开网页时没有图像?
- javascript - 在 Javascript 中,如何使用 fetch() 从一个 API 请求中读取多个流块?
- angular - Angular code splitting into multiple functional bundles
- reactjs - 我如何在循环中实现多个承诺
- docker - Botium-cli quickstart process it taking so long and long
- php - 自定义存档页面不适用于日期或标签,但适用于类别
- android-studio - What does "-keepattributes SourceFile,LineNumberTable" mean for the new Android code shrinker R8?
- python - update missing values in dataframe from another dataframe
- excel - Excel公式计算单元格值中有多少正斜杠(/)