1.0.0 • Published 4 years ago

recursive-object-key-filter v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

recursive-object-key-filter

Filter / Remove keys from object recursively.

Installation

if you are using npm, run

npm install --save recursive-object-key-filter

or

if you are using yarn package manager, run

yarn add recursive-object-key-filter

Example

Remove the key __typename from an object.

import filter from 'recursive-object-key-filter'; // ES6
// or
const filter = require('recursive-object-key-filter'); // ES5

const dirtyObject = {
  'id': '12345',
  'name': 'javascript',
  '__typename': 'language'
};

console.log(dirtyObject);
// outputs ==> { 'id': '12345', 'name': 'javascript', '__typename': 'language' }

const cleanObject = filter(dirtyObject, ['__typename']);

console.log(cleanObject);
// outputs ==> { 'id': '12345', 'name': 'javascript'}

Remove any of the keys __typename , id from an object.

import filter from 'recursive-object-key-filter'; // ES6
// or
const filter = require('recursive-object-key-filter'); // ES5

const dirtyObject = {
  'id': '12345',
  'name': 'javascript',
  '__typename': 'language',
  'books': [
    {
      'name': 'Learn JS',
      'author': 'Naveen Bharathi',
      '__typename': 'book'
    },
    {
      'name': 'A guide to TS',
      'author': 'Naveen',
      '__typename': 'book'
    }
  ]
};

console.log(dirtyObject);
/* outputs ==>  
  {
    'id': '12345',
    'name': 'javascript',
    '__typename': 'language',
    'books': [
      {
        'name': 'Learn JS',
        'author': 'Naveen Bharathi',
        '__typename': 'book'
      },
      {
        'name': 'A guide to TS',
        'author': 'Naveen',
        '__typename': 'book'
      }
    ]
  }
*/

const cleanObject = filter(dirtyObject, ['__typename', 'id']);

console.log(cleanObject);
/* outputs ==>  
  {
    'name': 'javascript',
    'books': [
      {
        'name': 'Learn JS',
        'author': 'Naveen Bharathi',
      },
      {
        'name': 'A guide to TS',
        'author': 'Naveen',
      }
    ]
  }
*/

Parameters

filter(obj, keys)

  • obj {Object | Array} - object you want to filter the keys from.
  • keys {Array} - Array of keys you want to filter / remove in the object passed.

LICENSE

MIT License

Copyright (c) 2020 Naveen Bharathi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.