A utility to flatten deeply nested objects based on specified keys.
$ npm install nested-to-flatconst nestedToFlat = require("nested-to-flat");
const tree = {
nom: "carlita",
age: 60,
enfant: {
nom: "rosa",
age: 42,
enfant: {
nom: "mamadou",
age: 16
}
}
};
const triggerKeys = ['enfant'];
const flattenedData = nestedToFlat(triggerKeys, tree);
console.log(flattenedData);Flatten an object based on the given keys.
Returns: Object[] - Flattened version of the object
| Param | Type | Description |
|---|---|---|
| triggerKeys | String[] |
Keys used to flat the given object. |
| object | Object |
Object to flatten. |
Example
const nestedToFlat = require("nested-to-flat");
const tree = {
nom: "carlita",
age: 60,
enfant: {
nom: "rosa",
age: 42,
enfant: {
nom: "mamadou",
age: 16
}
}
};
const triggerKeys = ['enfant'];
const flattenedData = nestedToFlat(triggerKeys, tree);
console.log(flattenedData);
// Output:
// [
// {
// nom: "carlita",
// age: 60
// },
// {
// nom: "rosa",
// age: 42
// },
// {
// nom: "mamadou",
// age: 16
// }
// ]To run the tests for the utility, use:
$ npm testTo check test coverage, use:
$ npm run test:coverageMIT © muceres