Objects
Data Types
Strings
export const transform = (source: Record<string, any>): Record<string, any> => { const target: Record<string, any> = Object.create(null); for (let [key, value] of Object.entries(source)) { const lastUnderscoreIndex = key.lastIndexOf("_"); const keyValid = key.length > 1; const valueKey = keyValid ? key.slice(lastUnderscoreIndex + 1) : key; let deepestNode = target; if (keyValid) { deepestNode = key.split("_") .slice(0, -1) .reduce((node: Record<string, any>, element: string) => node[element] ??= {}, target); } if (valueKey === '_') { // dont get why it should be value_d but ok value = 'value_d'; } deepestNode[valueKey] = value; } return target; }
- export const transform = (source: Record<string, any>): Record<string, any> => {
const target = Object.create(null);// TODO: handle invalid propertyObject.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;});- const target: Record<string, any> = Object.create(null);
- for (let [key, value] of Object.entries(source)) {
- const lastUnderscoreIndex = key.lastIndexOf("_");
- const keyValid = key.length > 1;
- const valueKey = keyValid ? key.slice(lastUnderscoreIndex + 1) : key;
- let deepestNode = target;
- if (keyValid) {
- deepestNode = key.split("_")
- .slice(0, -1)
- .reduce((node: Record<string, any>, element: string) => node[element] ??= {}, target);
- }
- if (valueKey === '_') {
- // dont get why it should be value_d but ok
- value = 'value_d';
- }
- deepestNode[valueKey] = value;
- }
- return target;
- }