Objects are added to nodeArray[] once they are selected, some of the nodes are connected together and these links are stored in linkArray[], each object holds the source ID and the target ID of the node connection.
I need to filter linkArray[] so that it only returns objects where both source and target are in nodeArray[].
So far from browsing similar questions i have the following:
var linkArray = [{
"conID": "100",
"source": "10",
"target": "11"
}, {
"conID": "101",
"source": "11",
"target": "12"
}, {
"conID": "102",
"source": "12",
"target": "13"
}, {
"conID": "103",
"source": "13",
"target": "14"
}, {
"conID": "386",
"source": "55",
"target": "32"
}];
var nodeArray = [{"id": "10"}, {"id": "11"}, {"id": "12"}];
function filterArray(array, filter) {
var myArrayFiltered = [];
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < filter.length; j++) {
if (array[i].source === filter[j].id) {
myArrayFiltered.push(array[i]);
}
}
}
return myArrayFiltered;
}
myArrayFiltered = filterArray(linkArray, nodeArray);
document.body.innerHTML = '<pre>'+ JSON.stringify(myArrayFiltered, null, 4) +'</pre>';
I have tried adding to the if statement to include the target ID as well but im not sure i am understanding it correctly.
The result is returning all links with source matching id in nodeArray[].
[
{
"conID": "100",
"source": "10",
"target": "11"
},
{
"conID": "101",
"source": "11",
"target": "12"
},
{
"conID": "102",
"source": "12",
"target": "13"
}
]
What i need help with is filtering the array so that it only returns objects where both source and target ID are in nodeArray[].
The desired result after selecting node 10, 11 and 12 will be just 2 objects because node 13 is not in the selection.
[
{
"conID": "100",
"source": "10",
"target": "11"
},
{
"conID": "101",
"source": "11",
"target": "12"
}
]
Hopefully that is clear, Thanks!