gpt4 book ai didi

reactjs - 如何根据键从 Typescript 的 Dictionary 数据结构中删除条目?

转载 作者:行者123 更新时间:2023-12-04 10:11:46 26 4
gpt4 key购买 nike

我使用的是字典数据结构,其中 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

检查此链接 playground

关于reactjs - 如何根据键从 Typescript 的 Dictionary 数据结构中删除条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com