Как я могу удалить запись из структуры данных словаря в Typescript на основе ключа? - PullRequest
2 голосов
/ 19 апреля 2020

Я использую структуру данных словаря, где Key - это Id, а значение - PersonInformation,

interface PersonInformation{
FirstName:string;
SecondName:string;
Age:string;
}

Как я могу удалить запись о человеке из Словаря на основе Id.

1 Ответ

2 голосов
/ 19 апреля 2020

если ваш словарь 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

проверьте эту ссылку в детская площадка

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...