Converts the JSON object to another structure of JSON
Your code had a lot of loops so I tried to squish it into one reducer for you. I think this will work;

var output = data.reduce(function (out, d) {
   var edges = [d.from, d.to].sort();
   var key = edges.join("&lt;-&gt;");
   var obj = out.find(function (o) { return o.name === key; });
   if (!obj) {
     obj = {
       name: key,
       from: 0,
       to: 0
     };
     out.push(obj);
   }
   obj.to += edges[0] === d.from ? d.count : 0;
   obj.from += edges[1] === d.from ? d.count : 0;

   return out;
 }, []);

console.log(output);


Output:

[
  {
    "name": "B&lt;-&gt;D",
    "from": 45,
    "to": 44
  },
  {
    "name": "A&lt;-&gt;B",
    "from": 247,
    "to": 254
  },
  {
    "name": "C&lt;-&gt;D",
    "from": 211,
    "to": 208
  },
  {
    "name": "A&lt;-&gt;C",
    "from": 41,
    "to": 46
  },
  {
    "name": "A&lt;-&gt;D",
    "from": 38,
    "to": 10
  },
  {
    "name": "B&lt;-&gt;C",
    "from": 55,
    "to": 56
  }
]


Here's an example on JsBin

Reducers are super powerful when you need to convert an array into a different array or value.

In this case I'm iterating over the data array, and creating the key (A&lt;=&gt;B) by sorting the from and to values.

Then I check the output to see if we've already used that key, if so we append to that existing object, if not then we create it.