为什么在JavaScript中只显示最后一个元素而不是所有元素
我试图从json数据中检索某些信息,并希望创建一个新的键值对数组。但它只返回最后一个元素,而不是所有元素 我的代码如下:为什么在JavaScript中只显示最后一个元素而不是所有元素,javascript,arrays,json,object,mapping,Javascript,Arrays,Json,Object,Mapping,我试图从json数据中检索某些信息,并希望创建一个新的键值对数组。但它只返回最后一个元素,而不是所有元素 我的代码如下: const input = { "file1": { "function1": { "calls": { "105": { "file": "file1", "function": "function2" }, "106": { "file":
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 = {"source": functionName, "target": callObject['function']}
//console.log(res); // here all elements get printed out
});
}
});
});
});
return res;
}
const result = transformData(input);
console.log(result) // only giving {source:"function2", target:"function2"}
[
{
source: "function1",
target: "function2"
},
{
source: "function1",
target: "function3"
},
{
source: "function2",
target: "function2"
}
]
因此,我需要新的源、目标对,其中源是文件function1、function2下的键。Target是键调用function2、function3、function2中嵌套键函数的值。在这里,文件和函数的数量将更多。但有些函数可能根本没有调用数据。
因此,结果如下所示:
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 = {"source": functionName, "target": callObject['function']}
//console.log(res); // here all elements get printed out
});
}
});
});
});
return res;
}
const result = transformData(input);
console.log(result) // only giving {source:"function2", target:"function2"}
[
{
source: "function1",
target: "function2"
},
{
source: "function1",
target: "function3"
},
{
source: "function2",
target: "function2"
}
]
有人能帮我得到正确的输出吗。谢谢您的时间。看起来res=需要像res+=一样,也就是说,不要每次都覆盖您的数组,而是将找到的下一项添加到数组中。我不确定您的对象结构有多大保证,但假设您希望迭代所有file*键并获得函数映射,这应该可以做到 常量输入= { 文件1:{ 职能1:{ 电话:{ 105: { 文件:file1, 功能:功能2 }, 106: { 文件:file1, 功能:功能3 } }, 要点:{ 106: 106 } }, 职能2:{ 电话:{ 109: { 文件:file1, 功能:功能2 } }, 要点:{ 109: 111 } }, 职能3:{ 调用:{}, 要点:{ 132: 135 } } } } 常量结果=[]; forconst输入键{ if key.includes'file'{ const functions=Object.keysinput[key]; 函数的常数函数{ 常量funcObject=输入[key][func]; 对于funcObject.calls中的常量调用{ const callObj=funcObject.calls[call]; push{source:func,target:callObj.function}; } } } }
console.logresult 您需要编辑一行,如下所示
res = [...res,{"source": functionName, "target": callObject['function']}]
非常感谢你的想法。这很有帮助。