{ "class": "go.GraphLinksModel",
"nodeDataArray": [
{"isGroup":true, "key":-1, "text":"SP", "xy":"-5 269.225"},
{"isGroup":true, "key":-2, "text":"DM", "xy":"300 243.26562500000003"},
{"key":100, "Data":"device", "group":-2},
{"key":101, "Data":"name : Device1", "group":-2},
{"key":102, "Data":"config", "group":-2},
{"key":103, "Data":"interface", "group":-2},
{"key":104, "Data":"Loopback", "group":-2},
{"key":105, "Data":"name : {loopback-intf}", "group":-2},
{"key":106, "Data":"description", "group":-2},
{"key":107, "Data":"_tags : merge", "group":-2},
{"key":108, "Data":"__text : Loopback{loopback-intf}", "group":-2},
{"key":109, "Data":"ip", "group":-2},
{"key":110, "Data":"address", "group":-2},
{"key":111, "Data":"primary", "group":-2},
{"key":112, "Data":"address : {ip-address}", "group":-2},
{"key":113, "Data":"mask : 255.255.255.255", "group":-2},
{"key":114, "Data":"_xmlns : http://someweb.org", "group":-2},
{"key":115, "Data":"_tags : merge", "group":-2},
{"key":116, "Data":"Loopback", "group":-2},
{"key":117, "Data":"name : {loopback-intf}", "group":-2},
{"key":118, "Data":"description", "group":-2},
{"key":119, "Data":"_tags : merge", "group":-2},
{"key":120, "Data":"__text : Loopback{loopback-intf}", "group":-2},
{"key":121, "Data":"ip", "group":-2},
{"key":122, "Data":"address", "group":-2},
{"key":123, "Data":"primary", "group":-2},
{"key":124, "Data":"address : {ip-address}", "group":-2},
{"key":125, "Data":"mask : 255.255.255.255", "group":-2},
{"key":126, "Data":"_xmlns : http://sometest.org", "group":-2},
{"key":127, "Data":"_tags : merge", "group":-2},
{"key":0, "Data":"container car", "group":-1},
{"key":1, "Data":"container v8_engine", "group":-1},
{"key":2, "Data":"leaf-list cylinder-arrangement", "group":-1},
{"key":3, "Data":"type string", "group":-1},
{"key":4, "Data":"max-elements 8", "group":-1},
{"key":5, "Data":"container other-parts", "group":-1},
{"key":6, "Data":"list per-cylinder-parts", "group":-1},
{"key":7, "Data":"leaf piston-diameter", "group":-1},
{"key":8, "Data":"type uint32", "group":-1},
{"key":9, "Data":"range 2000... 9000", "group":-1},
{"key":10, "Data":"container valves", "group":-1},
{"key":11, "Data":"leaf number", "group":-1},
{"key":12, "Data":"list position", "group":-1},
{"key":13, "Data":"type change", "group":-1}
],
"linkDataArray": [
{"from":100, "to":101},
{"from":100, "to":102},
{"from":102, "to":103},
{"from":103, "to":104},
{"from":104, "to":105},
{"from":104, "to":106},
{"from":106, "to":107},
{"from":106, "to":108},
{"from":104, "to":109},
{"from":109, "to":110},
{"from":110, "to":111},
{"from":111, "to":112},
{"from":111, "to":113},
{"from":103, "to":114},
{"from":103, "to":115},
{"from":103, "to":116},
{"from":116, "to":117},
{"from":116, "to":118},
{"from":118, "to":119},
{"from":118, "to":120},
{"from":116, "to":121},
{"from":121, "to":122},
{"from":122, "to":123},
{"from":123, "to":124},
{"from":123, "to":125},
{"from":103, "to":126},
{"from":103, "to":127},
{"from":0, "to":1},
{"from":1, "to":2},
{"from":2, "to":3},
{"from":2, "to":4},
{"from":0, "to":5},
{"from":5, "to":6},
{"from":6, "to":7},
{"from":7, "to":8},
{"from":7, "to":9},
{"from":6, "to":10},
{"from":10, "to":11},
{"from":10, "to":12},
{"from":5, "to":13},
{"category":"Mapping", "from":4, "to":116},
{"category":"Mapping", "from":6, "to":104},
{"category":"Mapping", "from":9, "to":116},
{"category":"Mapping", "from":12, "to":109}
]}
这是我的输入对象,我想通读并查找以下“类别”:“映射”,然后创建一个数组,如下所示
category:[{a:4,b:116},{a:6,b:104},{a:9,b:116},{a:12,b:109}]
但即使是简单的代码
console.log(data.linkData.Array);
将输出视为未定义。
注意:如果我传递此数据硬编码,如var data =该对象,它可以完美地工作但是同一个数组我动态分配它并尝试访问该元素,我得到了未分配。不确定是什么导致它:(
我也试过以下
var data = JSON.parse(JSON.stringify(savedDiagramModel));
然后访问数据但没有运气
答案 0 :(得分:1)
这将过滤和映射以获得您期望的结果:
var result = data.linkDataArray.filter(function(item) {
return item.category;
}).map(function(item) {
return {a: item.from, b: item.to};
});
/*
[{
a: 4,
b: 116
}, {
a: 6,
b: 104
}, {
a: 9,
b: 116
}, {
a: 12,
b: 109
}]
*/
答案 1 :(得分:1)
如果您想在纯Javascript中执行此操作:
var category = [];
for (var i=0; i < data.linkDataArray.length; i++) {
var o = data.linkDataArray[i];
if (typeof(o.category) !== 'undefined') category.push({ a:o.from, b:o.to });
}
答案 2 :(得分:0)
问题在于动态数据。
所以我必须在解析对象之前添加以下行。
let jsonObject =JSON.parse(savedDiagramModel);
然后添加一个通过linkDataArray解析的函数,然后根据需要创建一个对象。