Hey there!
Hello, I am new to working with js/d3.js and tackling a small project. Here are some of my previous inquiries:
- D3.js: Dynamically create source and target based on identical JSON values
- JS / d3.js: Steps for highlighting adjacent links
My current project involves dynamically generating source and target pairs based on user inputs. However, I have encountered an issue where there are duplicate links among nodes. Upon investigation, I noticed that the duplication may be stemming from repeated Source/Target elements in the array being created. Below is a snippet showing what happened within the 'links' array:
//Source and target serve as unique identifiers for each datastruct
source: S001A, target: S002A
source: S001A, target: S003A
source: S001A, target: S004A
source: S002A, target: S001A //Duplicate
source: S002A, target: S005A
source: S003A, target: S001A //Duplicate
source: S003A, target: S006A
source: S004A, target: S001A //Duplicate
...
The cause of this issue lies in my raw data having a nested array for "Friends". Here's an example entry in the dataset:
{
"NRIC": "S001A",
"name": "Benjamin",
"blk": 123,
"estate": "Woodlands",
"street": "Woodlands Street 12",
"unitNo": "01-23",
"postal": 123123,
"school": "Nanyang Technological University",
"Friends": //Nested array..
[
"S002A",
"S003A",
"S004A",
]
}
Below is the for-loop I've been utilizing to generate the source-target arrays for the nested data:
graphData.forEach(function(gdata,index)
{
for (i = 0; i < gdata.Friends.length; i++)
{
links.push({
source: gdata.NRIC,
target: gdata.Friends[i]
});
}
});
This loop inevitably leads to duplicates since friends are mutually included in each other's lists. (For instance, S001A is friends with S002A, S003A, and S004A - and vice versa).
I initially considered only using source/target pairs where source === this.id, but I fear this might result in omitting essential pairs and compromising data integrity.
Is there a way I can iterate through and eliminate these redundant pairings from the array? Whether by adjusting the existing for-loop or implementing post-processing on the data.
Thank you immensely for your guidance!