Skip to the content.

deeks - Deep Object Key Extraction

NPM version Downloads Minzipped Size Build Status Coverage Status Typings

Retrieve all keys and nested keys from objects and arrays of objects.

Installing

npm install --save deeks

Example:

const { deepKeys } = require('deeks');
// Alternatively:
// import { deepKeys } from 'deeks';

let generatedKeys = deepKeys({
	make: 'Nissan',
	model: 'GT-R',
	trim: 'NISMO',
	specifications: [
	    {mileage: 10},
	    {cylinders: 6}
	]
}, {
    expandArrayObjects: true,
    ignoreEmptyArraysWhenExpanding: true
});
// => ['make', 'model', 'trim', 'specifications.mileage', 'specifications.cylinders']

API

deepKeys

deepKeys(object, options)

Returns all keys in an object, even if they’re nested several layers deep. The array of keys that is returned can then be used with the doc-path module to get and set values at a specific key path.

Options (optional):

Returns: string[]

Example: ['make', 'model', 'specifications.odometer.miles', 'specifications.odometer.kilometers']

deepKeysFromList

deepKeysFromList(array, options)

Returns all keys in each object in the array, even if the keys are nested several layers deep in each of the documents. These can also be used with the doc-path module.

Options (optional):

Returns: string[][]

Example: [ ['make', 'model', 'specifications.odometer.miles', 'specifications.odometer.kilometers'] ]

Examples

This module integrates really nicely with the doc-path module, which allows the programmatic getting and setting of key paths produced by this module.

Additionally, doc-path@>=3 works with the keys returned when the escapeNestedDots option is specified.

Here’s an example of how this works:

const path = require('doc-path'),
      { deepKeys } = require('deeks');

let car = {
		make: 'Nissan',
		model: 'GT-R',
		trim: 'NISMO',
		specifications: {
			mileage: 10,
			cylinders: '6'
		}
	},
	
	carKeys = deepKeys(car);

for(let keyPath of carKeys) {
    // Clear all values
    path.setPath(car, keyPath, '');
}

Tests

$ npm test

Note: This requires mocha.

To see test coverage, please run:

$ npm run coverage