Skip to content

Instantly share code, notes, and snippets.

@nikolaswise
Created March 29, 2024 12:25
Show Gist options
  • Save nikolaswise/db31bcdabce51662d344c328034ae599 to your computer and use it in GitHub Desktop.
Save nikolaswise/db31bcdabce51662d344c328034ae599 to your computer and use it in GitHub Desktop.
Take a JSON-LD object and cast it into two arrays, one of nodes and one of links, for rendering with a D3 network visualization.
const fn = (jsonLD, linkType) => {
let nodes = jsonLD['@graph']
.filter(n => n)
.map(n => {
n.level = 0
return n
})
let connections = subjects["@graph"].map(node => {
return {
source: node.uri,
target: [...arrayify(node[linkType])].map(node => node.uri),
strength: 1.0
}
})
let links = []
connections.forEach(concept => {
concept.target.forEach(target => {
links.push({
source: concept.source,
target: target,
strength: 1
})
})
})
// { target: "mammal", source: "dog" , strength: 1.0 },
return {
nodes: [
...nodes,
],
links: links
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment