Skip to content

Instantly share code, notes, and snippets.

@sn-donbenjamin
Forked from aurbano/removeKeys.js
Created April 19, 2019 16:01
Show Gist options
  • Save sn-donbenjamin/87af28d30983fc6cc2992bbd7cedd119 to your computer and use it in GitHub Desktop.
Save sn-donbenjamin/87af28d30983fc6cc2992bbd7cedd119 to your computer and use it in GitHub Desktop.

Revisions

  1. @aurbano aurbano revised this gist Jul 2, 2015. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions removeKeys.js
    Original file line number Diff line number Diff line change
    @@ -7,19 +7,25 @@
    * @param keys An array of property names (strings) to remove
    */
    function removeKeys(obj, keys){
    var index;
    for (var prop in obj) {
    // important check that this is objects own property
    // not from prototype prop inherited
    if(obj.hasOwnProperty(prop)){
    switch(typeof(prop)){
    switch(typeof(obj[prop])){
    case 'string':
    var index = keys.indexOf(prop);
    index = keys.indexOf(prop);
    if(index > -1){
    delete obj[prop];
    }
    break;
    case 'object':
    removeKeys(prop, keys);
    index = keys.indexOf(prop);
    if(index > -1){
    delete obj[prop];
    }else{
    removeKeys(obj[prop], keys);
    }
    break;
    }
    }
  2. @aurbano aurbano revised this gist Jun 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion removeKeys.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * Remove all specified from an object, no matter how deep they are.
    * Remove all specified keys from an object, no matter how deep they are.
    * The removal is done in place, so run it on a copy if you don't want to modify the original object.
    * This function has no limit so circular objects will probably crash the browser
    *
  3. @aurbano aurbano created this gist Jun 16, 2015.
    27 changes: 27 additions & 0 deletions removeKeys.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /**
    * Remove all specified from an object, no matter how deep they are.
    * The removal is done in place, so run it on a copy if you don't want to modify the original object.
    * This function has no limit so circular objects will probably crash the browser
    *
    * @param obj The object from where you want to remove the keys
    * @param keys An array of property names (strings) to remove
    */
    function removeKeys(obj, keys){
    for (var prop in obj) {
    // important check that this is objects own property
    // not from prototype prop inherited
    if(obj.hasOwnProperty(prop)){
    switch(typeof(prop)){
    case 'string':
    var index = keys.indexOf(prop);
    if(index > -1){
    delete obj[prop];
    }
    break;
    case 'object':
    removeKeys(prop, keys);
    break;
    }
    }
    }
    }