score:3

Accepted answer

we can change your original array to an array without duplicates, and containing the number of original duplicates. we're gonna use that number to set the width of the links.

so, if this is your array:

var links = [{
    "source": a,
    "target": b
}, {
    "source": a,
    "target": b
}, {
    "source": b,
    "target": a
}, {
    "source": b,
    "target": c
}];

let's first put all the properties of each object in order:

links.foreach(function(d) {
    var sourcetemp = d.source, targettemp = d.target;
    if (d.source > d.target) {
        d.source = targettemp;
        d.target = sourcetemp;
    }
});

then, we'll count how many links are equal:

var counter = {};

links.foreach(function(obj) {
    var key = json.stringify(obj);
    counter[key] = (counter[key] || 0) + 1
});

and, then, we'll populate your final array:

var finalarray = [];

for (var key in counter) {
    var tempkey = key.substring(0, key.length - 1) + ",\"value\":" + counter[key] + "}";
    finalarray.push(tempkey)
};

as i used json.stringify for counting the duplicated objects, let's parse it:

finalarray.foreach(function(d, i, array) {
    array[i] = (json.parse(d))
})

so, at the end, if you log your finalarray, you'll get this:

[
    {"source": "a","target": "b","value": 3},           
    {"source": "b","target": "c","value": 1}
];

finally, define finalarray as the data for the links and use the value to set the width of your links:

.style("stroke-width", d => d.value)

check the console in this demo, using your original array:

var links = [{
        "source": "a",
        "target": "b"
    }, {
        "source": "a",
        "target": "b"
    }, {
        "source": "b",
        "target": "a"
    }, {
        "source": "b",
        "target": "c"
    }];

    links.foreach(function(d) {
        var sourcetemp = d.source, targettemp = d.target;
        if (d.source > d.target) {
            d.source = targettemp;
            d.target = sourcetemp;
        }
    });

    var counter = {};

    links.foreach(function(obj) {
        var key = json.stringify(obj);
        counter[key] = (counter[key] || 0) + 1
    });

    var finalarray = [];

    for (var key in counter) {
        var tempkey = key.substring(0, key.length - 1) + ",\"value\":" + counter[key] + "}";
        finalarray.push(tempkey)
    };

    finalarray.foreach(function(d, i, array) {
        array[i] = (json.parse(d))
    })

    console.log(finalarray);


Related Query

More Query from same tag