javascript - 如何根据边的数组对象内的数据选择边?

标签 javascript json cytoscape.js cytoscape-web

我正在尝试根据传入的字符串选择边。这是一个示例 json:

{
"group": "edges",
"data": {
  "id": "8",
  "source": "Q14814",
  "target": "P20393",
  "direction": "|->",
  "Sources": {
    "dataSource": "database",
    "dbId": "0",
    "sourceId": "1368140",
    "sourceType": "REACTION"
  }
}

每条边可能有一个源,或一个源数组。我想选择所有具有我传入的字符串的 sourceId 的边。因此,如果我传入一个字符串“1368140”,我想获取所有包含 sourceId 为“1368140”的源的边。

我尝试了几种不同的方法来选择它,包括:

cy.filter('edge[reactomeId = "' + string + '"]'));

cy.elements('edge[reactomeId = "' + string + '"]'));

cy.filter('reactomeId = ' + string + ''));

以及这方面的所有其他排列。有谁知道如何正确选择这些边?

最佳答案

这是我的做法:

// The syntax is important here!
cy.edges('[source = "' + string + '"]');

可以找到此特定语法的来源 here .

实例(控制台输出):

var cy = (window.cy = cytoscape({
  container: document.getElementById("cy"),

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: "node",
      css: {
        content: "data(id)",
        "text-valign": "center",
        "text-halign": "center",
        height: "60px",
        width: "100px",
        shape: "rectangle",
        "background-color": "data(faveColor)"
      }
    },
    {
      selector: "edge",
      css: {
        "curve-style": "bezier",
        "control-point-step-size": 40,
        "target-arrow-shape": "triangle"
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: "Top",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "yes",
          faveColor: "#37a32d"
        }
      },
      {
        data: {
          id: "no",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Third",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Fourth",
          faveColor: "#56a9f7"
        }
      }
    ],
    edges: [{
        data: {
          source: "Top",
          target: "yes"
        }
      },
      {
        data: {
          source: "Top",
          target: "no"
        }
      },
      {
        data: {
          source: "no",
          target: "Third"
        }
      },
      {
        data: {
          source: "Third",
          target: "Fourth"
        }
      },
      {
        data: {
          source: "Fourth",
          target: "Third"
        }
      }
    ]
  },
  layout: {
    name: "dagre"
  }
}));

cy.bind('click', 'node', function(event) {
  console.log(cy.edges('[source = "' + event.target.id() + '"]'));
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 85%;
  width: 100%;
  float: right;
  position: absolute;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://unpkg.com/cytoscape@3.3.0/dist/cytoscape.min.js">
  </script>
  <!-- cyposcape dagre -->
  <script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>

关于javascript - 如何根据边的数组对象内的数据选择边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070139/

相关文章:

c# - 日期转换: From LINQ to JSON to Javascript

cytoscape.js - cytoscape 以动画路径显示节点之间的流量

javascript - 任何 Javascript 图分析库都可以表明社区结构有多强?

javascript - 如何使用 cytoscape.js 在图形节点上的鼠标悬停事件上添加工具提示

javascript - 使用 Javascript 检索文本时遇到问题

javascript - 如何找出哪个包使用了不安全的 componentWillMount 方法?

javascript - 为什么 javascript 不能在 if 语句中使用 .style.color

java - 为什么在删除 JSON 对象时此循环会过早退出?

python - 数据重映射技术

javascript - 如果子 aria-expanded=true 将类添加到父元素