Skip to content

joaopaulomoraes/json-object-keys

Repository files navigation

json-object-keys

json-object-keys | ⚠️ WIP ⚠️

Manage complex object keys in depth.

Table of contents

Install

Bun

bun add json-object-keys

PNPM

pnpm add json-object-keys

NPM

npm install json-object-keys

Yarn

yarn add json-object-keys

Remove

Remove an unique key

import { remove } from 'json-object-keys'

remove(
  {
    id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
    name: 'John Doe',
    category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
    email: '[email protected]',
    age: 29,
    ...
  },
  'category'
)

Output

{
  id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
  name: 'John Doe',
- category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
  email: '[email protected]',
  age: 29
}

Remove multiple keys

import { remove } from 'json-object-keys'

remove(
  {
    id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
    name: 'John Doe',
    category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
    email: '[email protected]',
    age: 29
  },
  ['category', 'email']
)

Output

{
  id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
  name: 'John Doe',
- category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
- email: '[email protected]',
  age: 29
}

Replace

Replace an unique key

import { replace } from 'json-object-keys'

replace({ id: 1, foo: 2, baz: 3 }, { foo: 'bar' })

Output

{
  id: 1,
+ bar: 2,
  baz: 3
}

Replace multiple keys

import { replace } from 'json-object-keys'

replace(
  {
    uuid: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
    name: 'Grandchild 1',
    children: [
      {
        uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
        name: 'Great Grandchild 1'
      },
      {
        uuid: 'c579a0b8-c2c7-44e2-a7d9-2edba8f7b472',
        name: 'Great Grandchild 2'
      }
    ]
  }, { uuid: 'id', children: 'node' })

Output

{
+ id: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
  name: 'Grandchild 1',
+ node: [
    {
+     id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
      name: 'Great Grandchild 1'
    },
    {
+     id: 'c579a0b8-c2c7-44e2-a7d9-2edba8f7b472',
      name: 'Great Grandchild 2'
    }
  ]
}

License

This project is licensed under the MIT License.