nodejsdeveloperskh

flat object in javascript

Dec 9th, 2021 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as _ from 'lodash';
  2.  
  3. /**
  4.  *
  5.  * @param {object} value
  6.  * @returns {object}
  7.  */
  8. export function flattenObject(value) {
  9.     /**@type {object} */
  10.     let toReturn = {};
  11.  
  12.     for (const i in value) {
  13.         if (!value.hasOwnProperty(i)) {
  14.             continue;
  15.         }
  16.  
  17.         if (typeof value[i] == 'object') {
  18.             const flatObject = flattenObject(value[i]);
  19.             for (const x in flatObject) {
  20.                 if (!flatObject.hasOwnProperty(x)) continue;
  21.  
  22.                 toReturn[i + '.' + x] = flatObject[x];
  23.             }
  24.         } else {
  25.             toReturn[i] = value[i];
  26.         }
  27.     }
  28.     return toReturn;
  29. }
  30.  
  31.  
  32. /////////////////////OR//////////////////////////////
  33.  
  34. // @ts-check
  35.  
  36. /**
  37.  *
  38.  * @param {object} object
  39.  * @returns {Promise<object>}
  40.  */
  41. export async function flatObjectAndSeparateThemByDot(object) {
  42.     /**@type {object} */
  43.     const res = {};
  44.     /**
  45.      *
  46.      * @param {object} obj
  47.      * @param {string} [current]
  48.      */
  49.     (function recurse(obj, current) {
  50.         if (current && _.isArray(object[current])) {
  51.             // this condition will ignore arrays
  52.             res[current] = object[current];
  53.         } else {
  54.             for (const key in obj) {
  55.                 const value = obj[key];
  56.                 // joined keys with dot
  57.                 const newKey = current ? current + '.' + key : key;
  58.                 if (value && typeof value === 'object') {
  59.                     // it's a nested object, so do it again
  60.                     recurse(value, newKey);
  61.                 } else {
  62.                     // it's not an object, so set the property
  63.                     res[newKey] = value;
  64.                 }
  65.             }
  66.         }
  67.     })(object);
  68.  
  69.     return res;
  70. }
  71.  
  72. // Typescript version
  73. export async function flatObjectAndSeparateThemByDot(
  74.     object: any,
  75. ): Promise<any> {
  76.     const res: any = {};
  77.  
  78.     (function recurse(obj: any, current?: string) {
  79.         if (current && _.isArray(object[current])) {
  80.             // this condition will ignore arrays
  81.             res[current] = object[current];
  82.         } else {
  83.             for (const key in obj) {
  84.                 const value = obj[key];
  85.                 // joined keys with dot
  86.                 const newKey = current ? current + '.' + key : key;
  87.                 if (value && typeof value === 'object') {
  88.                     // it's a nested object, so do it again
  89.                     recurse(value, newKey);
  90.                 } else {
  91.                     // it's not an object, so set the property
  92.                     res[newKey] = value;
  93.                 }
  94.             }
  95.         }
  96.     })(object);
  97.  
  98.     return res;
  99. }
Add Comment
Please, Sign In to add comment