我有一个json数据如下
{
"file1": {
"function1": {
"calls": {
"439:0": [
"441:24"
],
"441:24": [
"443:4"
],
"443:4": [
"447:7",
"445:10"
],
"445:10": [
"449:4"
],
"447:7": [
"449:4"
]
}
},
"function2": {
"calls": {
"391:0": [
"393:8"
],
"393:8": [
"397:7"
],
"397:7": [
"395:27"
]
}
},
"function3": {
"calls": null
},
"function4": {
"calls": null
}
},
"file2": {
"function5": {
"calls": null
},
"function6": {
"calls": {
"391:0": [
"393:8"
],
"393:8": [
"397:7"
],
"397:7": [
"395:27"
]
}
}
}
}
我需要将其转换为“function1”的格式
{
"nodes": [
{
"id": "439:0",
"line": "439:0"
},
{
"id": "441:24",
"line": "441:24"
},
{
"id": "443:4",
"line": "443:4"
},
{
"id": "447:7",
"line": "447:7"
},
{
"id": "445:10",
"line": "445:10"
},
{
"id": "449:4",
"line": "449:4"
}
],
"links": [
{
"source": "439:0",
"target": "441:24"
},
{
"source": "441:24",
"target": "443:4"
},
{
"source": "443:4",
"target": "447:7"
},
{
"source": "443:4",
"target": "445:10"
},
{
"source": "445:10",
"target": "449:4"
},
{
"source": "447:7",
"target": "449:4"
}
]
}
其中“调用”键是“节点”中的行和 id,以及“链接”中的源。目标是调用内的值。如果任何键有多个值,而不是每个值,它将创建一个源-目标对。
这将在稍后用作 data.nodes.map(function(d){return d.line})
我尝试了以下代码,但它无法正常工作。它提供了一个包含正确信息的数组,但我需要在进一步的步骤中使用它的方式,它在那里不起作用。它给出诸如属性“nodes”在类型“any[]”上不存在
之类的错误。
let res = {}
let nodes = []
let links = []
Object.entries(input0).map(([fileName, fileObject]) => {
Object.entries(fileObject).map(([functionName, functionObject]) => {
if(functionName=="function1"){
Object.entries(functionObject).map(([functionKey, functionValue]) => {
if(functionKey === "calls") {
if(functionValue != null){
Object.entries(functionValue).map(([callKey, callObject]) => {
nodes = [...nodes,{"id": callKey, "line": callKey}]
callObject.forEach(x => {
links = [...links,{"source": callKey, "target": x}]
});
res = {"nodes": nodes, "links": links} //nodes.concat(links)
})
}
}
})
}
})
})
console.log(res)
请有人帮忙。感谢您抽出时间。
最佳答案
您可以省略不需要的属性并直接嵌套一次,并通过检查现有节点构建一组新的节点并分配所有链接而无需进一步检查。
function getNodesLinks(object, result = { nodes: [], links: [] }) {
if (object.calls === null) return result;
if (!object.calls) {
Object.values(object).forEach(v => getNodesLinks(v, result));
return result;
}
Object.entries(object.calls).forEach(([source, targets]) => {
if (!result.nodes.some(({ id }) => id === source)) {
result.nodes.push({ id: source, line: source });
}
targets.forEach(target => {
if (!result.nodes.some(({ id }) => id === target)) {
result.nodes.push({ id: target, line: target });
}
result.links.push({ source, target });
});
});
return result;
}
var data = { file1: { function1: { calls: { "439:0": ["441:24"], "441:24": ["443:4"], "443:4": ["447:7", "445:10"], "445:10": ["449:4"], "447:7": ["449:4"] } }, function2: { calls: { "391:0": ["393:8"], "393:8": ["397:7"], "397:7": ["395:27"] } }, function3: { calls: null }, function4: { calls: null } }, file2: { function5: { calls: null }, function6: { calls: { "391:0": ["393:8"], "393:8": ["397:7"], "397:7": ["395:27"] } } } },
result = getNodesLinks(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 通过JavaScript修改Json数据创建新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59930609/