Ad
Strings
Code
Diff
  • export const transform = (source: Record<string, any>): Record<string, any> => {
      return Object.fromEntries(
        Object.entries(source).map(([ key, value ]) => {
          const match = key.match(/^([^_]+)_(.+)$/);
          if (match) {
            return [ match[1], transform({ [match[2]]: value }) ];
          } else {
            return [ key, value ];
          }
        }),
      );
    }
    • 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;
    • return Object.fromEntries(
    • Object.entries(source).map(([ key, value ]) => {
    • const match = key.match(/^([^_]+)_(.+)$/);
    • if (match) {
    • return [ match[1], transform({ [match[2]]: value }) ];
    • } else {
    • return [ key, value ];
    • }
    • }),
    • );
    • }