如何根据键从 Typescript 中的字典数据结构中删除条目?

Sak*_*shi 7 dictionary typescript reactjs

我正在使用字典数据结构,其中键是 Id,值是 PersonInformation,

interface PersonInformation{
FirstName:string;
SecondName:string;
Age:string;
}
Run Code Online (Sandbox Code Playgroud)

如何根据 ID 从词典中删除某个人的条目。

Tag*_*ari 9

如果你的字典是一个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
Run Code Online (Sandbox Code Playgroud)
// 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
Run Code Online (Sandbox Code Playgroud)

在操场上检查此链接