MCPcopy Create free account
hub / github.com/Faheem-Khan97/javascript-coding-problems-with-theory / transform

Function transform

algo.js:39–69  ·  view source on GitHub ↗
(input, parentKey = "", res = {})

Source from the content-addressed store, hash-verified

37};
38
39function transform(input, parentKey = "", res = {}) {
40 Object.keys(input).forEach((key) => {
41 const value = input[key];
42 if (value && Array.isArray(value)) {
43 const pkey = parentKey == "" ? parentKey + key : parentKey + "." + key;
44 value.forEach((element, index) => {
45 const result = transform(value[index], pkey, {});
46 console.log({ result });
47 res[mapKeys[pkey]] = [...(res[mapKeys[pkey]] || []), result];
48 });
49 /* const result = transform(value, pkey, {})
50 console.log({result}) */
51 } else if (value && typeof value == "object") {
52 /* console.log({value})
53 console.log({parentKey}) */
54 transform(
55 value,
56 parentKey == "" ? parentKey + key : parentKey + "." + key,
57 res
58 );
59 } else {
60 /* console.log("inside else", parentKey, key, value) */
61 if (parentKey == "") {
62 res[mapKeys[parentKey + key]] = value;
63 } else {
64 res[mapKeys[parentKey + "." + key]] = value;
65 }
66 }
67 });
68 return res;
69}
70
71console.log(transform(inputObject, ""));

Callers 1

algo.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected