I am facing an issue while attempting to extract specific information from a JSON data and create a new array with key-value pairs. However, instead of getting all the elements, it only returns the last one.
Here is my current code snippet:
const input = {
"file1": {
"function1": {
"calls": {
"105": {
"file": "file1",
"function": "function2"
},
"106": {
"file": "file1",
"function": "function3"
}
},
"points": {
"106": "106"
}
},
"function2": {
"calls": {
"109": {
"file": "file1",
"function": "function2"
}
},
"points": {
"109": "111"
}
},
"function3": {
"calls": {},
"points": {
"132": "135"
}
}
}
}
function transformData(input) {
let res = [];
Object.entries(input).map(([fileName, fileObject]) => {
Object.entries(fileObject).map(([functionName, functionObject]) => {
Object.entries(functionObject).map(([functionKey, functionValue]) => {
if (functionKey === "calls") {
Object.entries(functionValue).map(([callKey, callObject]) => {
res.push({"source": functionName, "target": callObject['function']});
});
}
});
});
});
return res;
}
const result = transformData(input);
console.log(result);
The desired output should be an array with source-target pairs where the source corresponds to the key under 'file' (e.g., function1, function2) and the target is the value of the nested key 'function' within the 'calls' key (e.g., function2, function3, function2).
[
{
source: "function1",
target: "function2"
},
{
source: "function1",
target: "function3"
},
{
source: "function2",
target: "function2"
}
]
If anyone could provide guidance on achieving the correct output, it would be greatly appreciated. Thank you for your time.