Skip to content

WebForgeOSS/nested-to-flat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nested To Flat

A utility to flatten deeply nested objects based on specified keys.

tested with jest license: MIT

Install

$ npm install nested-to-flat

Usage

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);

API

nestedToFlat(triggerKeys, object) ⇒ Object[]

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
//   }
// ]

Testing

To run the tests for the utility, use:

$ npm test

To check test coverage, use:

$ npm run test:coverage

License

MIT © muceres

About

flat an object based on a given keys

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published