# keys-in-object

> Find if the provided key of an object exists and retrieve the value(s)

Latest version **1.3.1** (published 2024-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install keys-in-object
pnpm add keys-in-object
yarn add keys-in-object
bun add keys-in-object
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2024-11-26 |
| First published | 2018-12-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Vangelis Makrynikolas |
| Maintainers | macvag |
| Keywords | Object keys, Nested Object, Object Values |

## Links

- npm: https://www.npmjs.com/package/keys-in-object
- Repository: https://github.com/macvag/KeysInObject
- Homepage: https://github.com/macvag/KeysInObject#readme
- Issues: https://github.com/macvag/KeysInObject/issues
- npm.io page: https://npm.io/package/keys-in-object

## Recent versions

- 1.3.1 (latest) — 2024-11-26
- 1.3.0 — 2020-04-21
- 1.1.1 — 2019-02-09
- 1.1.0 — 2018-12-29
- 1.0.4 — 2018-12-26
- 1.0.3 — 2018-12-26
- 1.0.2 — 2018-12-25
- 1.0.0 — 2018-12-25

## README

# Keys In Object

A Javascript library for retrieveing easy Object key's values.

## Installation

```
npm install keys-in-object
```

## How to use it

Node.js

```
var keysInObject = require('keys-in-object');

var users = {
	id_1: {
        name: 'John Doe',
        email: 'johndoe@test.com'
    },
	id_2: {
        name: 'Hohn Moe',
        email: 'hohnmoe@test.com'
    },
	id_3: {
        name: 'Joon Doo',
        email: 'joondoo@test.com'
    },
    id_4: {
        user_info: {
            name: 'Johnny Foe',
            email: 'johnnyfoe@test.com'
        }
    }
};

var arrayOfUserNames = keysInObject(users, 'name');
console.log(arrayOfUserNames)
// ['John Doe', 'Hohn Moe', 'Joon Doo', 'Johnny Foe']
```

Also you can pass an Array of Objects

```
var keysInObject = require('keys-in-object');

var user = {
	id_1: {
        name: 'John Doe',
        email: 'johndoe@test.com'
    },
	id_2: {
        name: 'Hohn Moe',
        email: 'hohnmoe@test.com'
    },
	id_3: {
        name: 'Joon Doo',
        email: 'joondoo@test.com'
    },
    id_4: {
        user_info: {
            name: 'Johnny Foe',
            email: 'johnnyfoe@test.com'
        }
    }
};

var usersArray = [user, user];
var arrayOfUserNames = keysInObject(usersArray, 'name');
console.log(arrayOfUserNames)
//[
      ['John Doe', 'Hohn Moe', 'Joon Doo', 'Johnny Foe'],
      ['John Doe', 'Hohn Moe', 'Joon Doo', 'Johnny Foe']
  ]
```

Browser

A build folder exists in keys-in-object under node_modules
Include the keysInObject.js file into your html script tags

```
<!doctype html>
<html>
    <head>
    </head>
    <body>
        <script src="./path to file/keysInObject.js"></script>
        <script>
            var users = {
	            id_1: {
                    name: 'John Doe',
                    email: 'johndoe@test.com'
                },
	            id_2: {
                    name: 'Hohn Moe',
                    email: 'hohnmoe@test.com'
                },
	            id_3: {
                    name: 'Joon Doo',
                    email: 'joondoo@test.com'
                },
                id_4: {
                    user_info: {
                        name: 'Johnny Foe',
                        email: 'johnnyfoe@test.com'
                    }
                }
            };

            var arrayOfUserNames = keysInObject(users, 'name');
            console.log(arrayOfUserNames)
        </script>
    </body>
</html>
```

## Additional feature

If you want to strip the unnecessary keys from the object and also to keep it's structure you can pass an optional Boolean Variable.

```
var userNames = keysInObject(users, 'name', true);
console.log(userNames)
/*
{
  id_1: {
    name: 'John Doe'
  },
  id_2: {
  	name: 'Hohn Moe'
  },
  id_3: {
  	name: 'Joon Doo'
  },
  id_4: {
    user_info: {
      name: 'Johnny Foe'
    }
  }
}
*/

// Or if you request for user_info
var userInfo = keysInObject(users, 'user_info', true);
console.log(userInfo)
/*
{
  id_4:{
    user_info: {
      name: 'Johnny Foe',
      email: 'johnnyfoe@test.com'
    }
  }
}
*/
```

## Tests

Simply clone the repo, npm install, and run npm test

---
_Source: https://npm.io/package/keys-in-object · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
