Ad
Objects
Data Types
Strings
Code
Diff
  • export const transform = (source: Record<string, any>): Record<string, any> => {
      
      // Array of keys
      const keys = Object.keys(source);
      
      // Array of splitted keys
      const splitted_keys: string[][] = [];
      
      // Splitting keys
      keys.forEach((item, i) => {
        
        // If name isn't underscore only
        if (item !== '_') {
          splitted_keys.push(item.split('_'));
          
        // If name is underscore only
        } else {
          splitted_keys.push(['_']);
        }
      });
      
      // Resulting object
      const result = Object.create(null);
      
      // Creating objects
      splitted_keys.forEach((item, index) => {
        
        // Merging objects
        Object.assign(result,
          recursiveCreateObject(
          
            // New object
            Object.create(null),
          
            // Array of keys
            item,
          
            // Final value
            source[keys[index]]
          )
        );
      });
      
      return result;
    }
    
    /**
     * @describe Function, that recursively creates objects
     * @param obj Object, that will be nested
     * @param keys Keys that will be used in nesting
     * @param final_value Final value, that will be used, when keys array will be empty
     * @returns Nested object
     */
    const recursiveCreateObject = (obj: any, keys: string[], final_value: any): any => {
      
      // If no keys in array
      if (keys.length === 0) {
        return obj[keys[0]] = final_value;
      }
      
      // Shift first value in array
      const shifted_symbol: string = keys.shift() as string;
      
      // Creating new nested object
      obj = {[shifted_symbol]: recursiveCreateObject(Object.create(null), keys, final_value)};
      
      // Returning nested object
      return obj;
    }
    • export const transform = (source: Record<string, any>): Record<string, any> => {
    • const target = Object.create(null);
    • // TODO: handle invalid property
    • Object.entries(source).forEach(([key, value]) => {
    • key.split("_").slice(0, -1).reduce((node: Record<string, any>, element: string) => {
    • return node[element] ??= {};
    • }, target)[key.slice(key.lastIndexOf("_") + 1)] = value;
    • });
    • return target;
    • // Array of keys
    • const keys = Object.keys(source);
    • // Array of splitted keys
    • const splitted_keys: string[][] = [];
    • // Splitting keys
    • keys.forEach((item, i) => {
    • // If name isn't underscore only
    • if (item !== '_') {
    • splitted_keys.push(item.split('_'));
    • // If name is underscore only
    • } else {
    • splitted_keys.push(['_']);
    • }
    • });
    • // Resulting object
    • const result = Object.create(null);
    • // Creating objects
    • splitted_keys.forEach((item, index) => {
    • // Merging objects
    • Object.assign(result,
    • recursiveCreateObject(
    • // New object
    • Object.create(null),
    • // Array of keys
    • item,
    • // Final value
    • source[keys[index]]
    • )
    • );
    • });
    • return result;
    • }
    • /**
    • * @describe Function, that recursively creates objects
    • * @param obj Object, that will be nested
    • * @param keys Keys that will be used in nesting
    • * @param final_value Final value, that will be used, when keys array will be empty
    • * @returns Nested object
    • */
    • const recursiveCreateObject = (obj: any, keys: string[], final_value: any): any => {
    • // If no keys in array
    • if (keys.length === 0) {
    • return obj[keys[0]] = final_value;
    • }
    • // Shift first value in array
    • const shifted_symbol: string = keys.shift() as string;
    • // Creating new nested object
    • obj = {[shifted_symbol]: recursiveCreateObject(Object.create(null), keys, final_value)};
    • // Returning nested object
    • return obj;
    • }