New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

deeply-nested

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deeply-nested

Get information about deeply nested keys in JavaScript objects and arrays

latest
Source
npmnpm
Version
1.1.3
Version published
Maintainers
1
Created
Source

deeply-nested deeply-nested

Get information about deeply nested keys in JavaScript objects and arrays

Install

$ npm install deeply-nested

Usage

ES Module

import { countKey, hasKey, findPaths, getValues } from "deeply-nested";

CommonJS

const { countKey, hasKey, findPaths, getValues } = require("deeply-nested");

Examples

import { countKey, hasKey, findPaths, getValues } from "deeply-nested";

const deeplyNestedObj = {
  a: {
    b: {
      c: {
        d: {
          e: "e",
          f: "f",
        },
      },
    },
    G: undefined,
    aFunction: () => {
      return true;
    },
  },
  d: {
    e: {
      f: "F",
    },
  },
  e: "Eeee",
};

// ** hasKey **

hasKey(deeplyNestedObj, "a");
// => true

hasKey(deeplyNestedObj, "x");
// => false

// ** countKey **

countKey(deeplyNestedObj, "a");
// => 1

countKey(deeplyNestedObj, "e");
// => 3

countKey(deeplyNestedObj, "x");
// => 0

// ** findPaths **

findPaths(deeplyNestedObj, "a");
// => [ 'a' ]

findPaths(deeplyNestedObj, "e");
// => [ 'e', 'a.b.c.d.e', 'd.e' ]

findPaths(deeplyNestedObj, "x");
// => []

// ** getValues **

getValues(deeplyNestedObj, "b");
// => [ { c: { d: [Object] } } ]

getValues(deeplyNestedObj, "e");
// => [ 'Eeee', 'e', { f: 'F' } ]

getValues(deeplyNestedObj, "G");
// => [ undefined ]

getValues(deeplyNestedObj, "aFunction");
// => [ [Function: aFunction] ]

getValues(deeplyNestedObj, "aFunction")[0]();
// => true

APIs

hasKey(obj, key)

Returns: Boolean

Returns true or false depending on whether the key is found in the nested object.

obj

Type: Object (or Array)

key

Type: String

Example

hasKey(obj, "id"); // => true

countKey(obj, key)

Returns: Number

Returns the number of occurrences of the key in the nested object.

obj

Type: Object (or Array)

key

Type: String

Example

countKey(obj, "name"); // => 3

findPaths(obj, key)

Returns: String[]

Returns an array of all the paths where the key occurs in the nested object, as dot-delimited strings (e.g., 'a.b.c.d.e').

obj

Type: Object (or Array)

key

Type: String

Example

findPaths(obj, "email");
// => [ 'employee.addresses[0].email', 'employee.addresses[1].email' ]

// The paths are `lodash.get` compatible:
import _ from "lodash";

const path = findPaths(obj, "provider");
// => [ 'user.profile.addresses[0].emails[0].provider'];

_.get(obj, path[0]);
// => 'gmail'

getValues(obj, key)

Returns: Any[]

Returns an array of all the values corresponding to the key's occurrences in the nested object.

obj

Type: Object (or Array)

key

Type: String

Example

getValues(obj, "age"); // => [ 25, 30, 45 ]

Additional Resources

This library implementation is based on depth-first search algorithms I originally shared in my blog posts for confirming the existence of nested keys and finding their paths in JavaScript:

  • Check if Key Exists in Deeply Nested Object or Array of Objects
  • Find Path of Key In Deeply Nested Object or Array

License

MIT © Talha Awan

Keywords

get

FAQs

Package last updated on 07 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts