Never Perfectly Managed

    qtools-object-flattener

    1.0.3 • Public • Published

    qtools-object-flattener

    Collapses JS complex, multi-level JS object to single level object with dotted path property names

    Usage:

    Flatten an object

    const jsonFlattener = require('qtools-object-flattener');
    
    const flatObject=jsonFlattener.convert(someObject, [options], [callback]);
    

    Works when executed on an Array but it doesn't make too much sense.

    Without a callback, the result is returned.

    Flatten an array of objects

    const flatArray=jsonFlattener.convertArray(someArrayOfObjects, [options], [callback]); 
    

    Optional parameters in the options parameter object.

    nameTransformer - a function that operates on the generated, dotted path in any way that is useful and produces a new string maxDepth - recursion limit, default is 20

    EG,

    const flatArray=jsonFlattener.convertArray(someArrayOfObjects, {nameTransformer:dottedPath>dottedPath.replace(/\./g, '\_')}); 
    
    const flatObject=jsonFlattener.convert(someObject, {nameTransformer:dottedPath=>dottedPath.replace(/\./g, '\_')});
    

    The closest there is to a test is:

    npm run demo

    Which has examples of the main function as illustrated below.

    Examples:

    Flattening an object:

    const flatObject=jsonFlattener.convert(someObject);
    

    someObject={ name: 'TQ White II', tv1:{ brand: 'Sony', inches: 60, internet: true }, websites:[ 'https://tqwhite.com', 'https://tech.genericWhite.com', 'https://jsLightning.com' ] }

    Flattens to:

    const flatObject={ name: 'TQ White II', 'tv1.brand': 'Sony', 'tv1.inches': 60, 'tv1.internet': true, 'websites[0]': 'https://tqwhite.com', 'websites[1]': 'https://tech.genericWhite.com', 'websites[2]': 'https://jsLightning.com' }

    Flattening an array of objects into an array of flattened objects

    const flatObject=jsonFlattener.convert(someObject);
    

    const someArray = [ { name: 'TQ White II', tv1: { brand: 'Sony', inches: 60, internet: true }, websites: [ 'https://tqwhite.com', 'https://tech.genericWhite.com', 'https://jsLightning.com' ] }, { name: 'Debbie White', tv1: { brand: 'Sony', inches: 60, internet: true }, websites: [ 'https://tqwhite.com', 'https://tech.genericWhite.com', 'https://jsLightning.com' ] } ];

    Flattens to:

    const flattenedArray=[ { name: 'TQ White II', 'tv1.brand': 'Sony', 'tv1.inches': 60, 'tv1.internet': true, 'websites[0]': 'https://tqwhite.com', 'websites[1]': 'https://tech.genericWhite.com', 'websites[2]': 'https://jsLightning.com' }, { name: 'Debbie White', 'tv1.brand': 'Sony', 'tv1.inches': 60, 'tv1.internet': true, 'websites[0]': 'https://tqwhite.com', 'websites[1]': 'https://tech.genericWhite.com', 'websites[2]': 'https://jsLightning.com' } ]

    Keywords

    none

    Install

    npm i qtools-object-flattener

    DownloadsWeekly Downloads

    1

    Version

    1.0.3

    License

    ISC

    Unpacked Size

    8.85 kB

    Total Files

    4

    Last publish

    Collaborators

    • tqwhite