Skip to content

Extracts multiple keys from any depth within an object and returns them in a flattened structure

License

Notifications You must be signed in to change notification settings

m00nbyte/data-extractor

Repository files navigation

data-extractor

npm version npm downloads


Description

Effortlessly extract multiple keys from deeply nested structures within an object, resulting in a reduced, flattened output.

Installation

npm install -D @m00nbyte/data-extractor
yarn add -D @m00nbyte/data-extractor

Usage

import extractData from '@m00nbyte/data-extractor';

const sourceObj = {
    level1: {
        '- quoted key -': 'yes',
        level2: {
            id: 'bc04be50',
            level3: {
                test: 'something',
                level4: {
                    arr: ['hello', [123, { test: true }]]
                }
            }
        }
    }
};

const pathMap = {
    quoted: 'level1.- quoted key -',
    id: 'level1.level2.id',
    test: 'level1.level2.level3.test',
    arr_0: 'level1.level2.level3.level4.arr[0]',
    arr_1_0: 'level1.level2.level3.level4.arr[1][0]',
    test2: 'level1.level2.level3.level4.arr[1][1].test',
    almeria: 'level1.level2.level3.level4.level5.almeria'
};

// optional
const defaultValue = 'not-found';

const extractedKeys = extractData(sourceObj, pathMap, defaultValue);

if (extractedKeys.id && extractedKeys.test) {
    // do something
}

Parameters

sourceObject

Type: Object

The source object to extract data from.

pathMap

Type: Object

An object mapping property names to their paths in the source object.

defaultValue

Type: String|undefined

Default: undefined

An optional default value to return if a key does not exist in the source object.

Contribution

Feel free to submit issues or pull requests.

Like my work?

This project needs a ⭐ from you. Don't forget to leave a star.

Buy Me A Coffee

About

Extracts multiple keys from any depth within an object and returns them in a flattened structure

Resources

License

Stars

Watchers

Forks

Packages

No packages published