I have
let Remove = ["Last", "Gender"];
let Contact = [{"First": "Bob", "Last": "Jim", "Gender": "M"},
{"First": "Amy", "Last": "Christen", "Gender": "F"}];
How I remove multiple keys from the dictionary with the keys I want to remove stored in an array?
To get
[{"First": "Bob"}, {"First": "Amy"}]
3
Answers
You could map the objects and reduce the copy by removing keys.
Here is a TS implementation. You can use a
Record<string, string>
type to represent your object.Another possible solution using forEach and for cycles:
You can use
.map()
on your array to map each object to a new object without the keys specified inremove
. For each object, you can grab the entries which you can then filter to remove the entries that have akey
within yourremove
array. Once you’ve removed the entries you don’t need, you can rebuild a new object by usingObject.fromEntries()
:TS Playground Link