我正在研究呈现为 json 数组的网络图代码。我需要更改代码以提供从另一个文件接收的数组数据。我拥有的代码:
var json = [
{
"adjacencies":
[
{
"nodeTo": "A",
"nodeFrom": "B",
"data":
{
"$color": "#557EAA"
}
}
],
"data":
{
"$color": "#909291",
"$type": "circle",
"$dim": 20
},
"id": "B",
"name": "B"
},
{
"adjacencies":
[
{
"nodeTo": "C",
"nodeFrom": "D",
"data":
{
"$color": "#416D9C"
}
}
],
"data":
{
"$color": "#83548B",
"$type": "circle",
"$dim": 20
},
"id": "D",
"name": "D"
},
];
我需要创建一个从 1 到 x 的 for 循环,并且在每次迭代中,我在 json 数组中添加一个值。(一个值由“adjacencies”、“data”、“id”、“name”组成)
请注意,nodeTo、nodeFrom、id 和 name 值存储在另一个数组中,因此在循环中我可以设置它们的值。
提前致谢:)
我已经尝试了 .push 如下: var json = []; var 邻接 = [];
for (i = 0; i < count; i++) {
var adjacencies = [];
var source = impactsString[i].Source;
var target = impactsString[i].Target;
var number = impactsString[i].NumberOfImpacts;
adjacencies.push({ nodeTo: target, nodeFrom: source });
json.push({ adjacencies: adjacencies, id: source, name: source });
}
但仍然无法正常工作