skip to Main Content

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"}]  

TS Playground

3

Answers


  1. You could map the objects and reduce the copy by removing keys.

    const remove = ["Last", "Gender"];
    const contacts = [
      {"First": "Bob", "Last": "Jim", "Gender": "M"}, 
      {"First": "Amy", "Last": "Christen", "Gender": "F"}
    ];
    
    const removed = contacts.map(contact => {
      return remove.reduce((copy, keyToRemove) => {
        delete copy[keyToRemove];
        return copy;
      }, { ...contact });
    });
    
    console.log(removed);
    .as-console-wrapper { top: 0; max-height: 100% !important; }

    Here is a TS implementation. You can use a Record<string, string> type to represent your object.

    const remove: string[] = ["Last", "Gender"];
    
    const contacts = [
      {"First": "Bob", "Last": "Jim", "Gender": "M"}, 
      {"First": "Amy", "Last": "Christen", "Gender": "F"}
    ];
    
    const removed = prune(contacts, remove);
    
    console.log(removed);
    
    function prune<TObj extends Record<string, string>>(
            records: TObj[], keysToRemove: string[]): TObj[] {
        return records.map(contact => {
            return keysToRemove.reduce((copy: TObj, keyToRemove: string) => {
                delete copy[keyToRemove]; // Remove they key from the copy
                return copy;
            }, { ...contact });
        });
    }
    
    Login or Signup to reply.
  2. Another possible solution using forEach and for cycles:

    let Remove = ["Last", "Gender"];
    
    let Contact = [{"First": "Bob", "Last": "Jim", "Gender": "M"}, 
                    {"First": "Amy", "Last": "Christen", "Gender": "F"}];
    
    //Want [{"First": "Bob"}, {"First": "Amy"}]      
    Contact.forEach((oneContact) => {
      for (let i=0; i<Remove.length; i++) {
        delete oneContact[Remove[i]];
      }
    })
    console.log(Contact);
    .as-console-wrapper { top: 0; max-height: 100% !important; }
    Login or Signup to reply.
  3. You can use .map() on your array to map each object to a new object without the keys specified in remove. For each object, you can grab the entries which you can then filter to remove the entries that have a key within your remove array. Once you’ve removed the entries you don’t need, you can rebuild a new object by using Object.fromEntries():

    const res = contact.map(obj => Object.fromEntries(Object.entries(obj).filter(([key]) => !remove.includes(key))));
    
    const remove = ["Last", "Gender"];
    const contact = [{"First": "Bob", "Last": "Jim", "Gender": "M"}, {"First": "Amy", "Last": "Christen", "Gender": "F"}];
    
    const res = contact.map(obj => Object.fromEntries(
      Object.entries(obj)
        .filter(([key]) => !remove.includes(key))
    ));
    console.log(res);

    TS Playground Link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search