Effortlessly extract multiple keys from deeply nested structures within an object, resulting in a reduced, flattened output.
npm install -D @m00nbyte/data-extractor
yarn add -D @m00nbyte/data-extractor
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
}
Type: Object
The source object to extract data from.
Type: Object
An object mapping property names to their paths in the source object.
Type: String|undefined
Default: undefined
An optional default value to return if a key does not exist in the source object.
Feel free to submit issues or pull requests.
This project needs a ⭐ from you. Don't forget to leave a star.