I have an array of objects like this one:
const data = [
{ source: "actor0", target: "actor1", value: 2 },
{ source: "actor0", target: "actor2", value: 1 },
{ source: "actor0", target: "actor3", value: 2 },
{ source: "actor0", target: "actor4", value: 3 },
{ source: "actor1", target: "actor0", value: 1 },
{ source: "actor1", target: "actor2", value: 1 },
{ source: "actor1", target: "actor3", value: 3 },
{ source: "actor1", target: "actor4", value: 1 },
{ source: "actor2", target: "actor0", value: 3 },
{ source: "actor2", target: "actor1", value: 1 },
{ source: "actor3", target: "actor0", value: 1 },
{ source: "actor3", target: "actor1", value: 2 },
{ source: "actor3", target: "actor2", value: 1 },
{ source: "actor4", target: "actor0", value: 2 },
{ source: "actor4", target: "actor2", value: 1 },
{ source: "actor4", target: "actor3", value: 2 }
];
It represents some links of a network. Since my network is not directional, I'm not interested to links with same source ad target.
I mean, I have { source: "actor0", target: "actor1", value: 2 }
so I can remove { source: "actor2", target: "actor0", value: 3 }
(value
is not important).
So the result should be:
const filteredData = [
{ source: "actor0", target: "actor1", value: 2 },
{ source: "actor0", target: "actor2", value: 1 },
{ source: "actor0", target: "actor3", value: 2 },
{ source: "actor0", target: "actor4", value: 3 },
// { source: "actor1", target: "actor0", value: 1 },
{ source: "actor1", target: "actor2", value: 1 },
{ source: "actor1", target: "actor3", value: 3 },
{ source: "actor1", target: "actor4", value: 1 },
// { source: "actor2", target: "actor0", value: 3 },
// { source: "actor2", target: "actor1", value: 1 },
// { source: "actor3", target: "actor0", value: 1 },
// { source: "actor3", target: "actor1", value: 2 },
{ source: "actor3", target: "actor2", value: 1 },
// { source: "actor4", target: "actor0", value: 2 },
{ source: "actor4", target: "actor2", value: 1 },
{ source: "actor4", target: "actor3", value: 2 }
];
How can I do that? I know it should be simple but I have no idea how to do that. Maybe using a Set
but how?
Thanks a lot
CodePudding user response:
You have to filter out your items, based on a particular find
condition. The condition being your source and target are matching or not.
Take each individual item from the array and search the array again based on that item. If the indices match, then we have the same item, so you can allow it. But if not, then you can discard the item,
This can be done using filter()
and find()
:
const data = [
{ source: "actor0", target: "actor1", value: 2 },
{ source: "actor0", target: "actor2", value: 1 },
{ source: "actor0", target: "actor3", value: 2 },
{ source: "actor0", target: "actor4", value: 3 },
{ source: "actor1", target: "actor0", value: 1 },
{ source: "actor1", target: "actor2", value: 1 },
{ source: "actor1", target: "actor3", value: 3 },
{ source: "actor1", target: "actor4", value: 1 },
{ source: "actor2", target: "actor0", value: 3 },
{ source: "actor2", target: "actor1", value: 1 },
{ source: "actor3", target: "actor0", value: 1 },
{ source: "actor3", target: "actor1", value: 2 },
{ source: "actor3", target: "actor2", value: 1 },
{ source: "actor4", target: "actor0", value: 2 },
{ source: "actor4", target: "actor2", value: 1 },
{ source: "actor4", target: "actor3", value: 2 }
];
let ans = data.filter((x,ind) => {
const findI = data.findIndex((y) => {
if(y.source === x.source && y.target === x.target) return true;
if(y.target === x.source && y.source === x.target) return true;
else return false;
});
if(findI === ind) return true;
});
console.log(ans);
CodePudding user response:
You could use Array.reduce()
to remove the duplicates in the data
array.
We'd create a grouping key based on source
and target
. We'll sort to ensure that order is not relevant, that is 'actor0-actor1' is the same key as 'actor1-actor0'.
Once we've grouped the items, we'll convert to an array again with Object.values()
const data = [ { source: "actor0", target: "actor1", value: 2 }, { source: "actor0", target: "actor2", value: 1 }, { source: "actor0", target: "actor3", value: 2 }, { source: "actor0", target: "actor4", value: 3 }, { source: "actor1", target: "actor0", value: 1 }, { source: "actor1", target: "actor2", value: 1 }, { source: "actor1", target: "actor3", value: 3 }, { source: "actor1", target: "actor4", value: 1 }, { source: "actor2", target: "actor0", value: 3 }, { source: "actor2", target: "actor1", value: 1 }, { source: "actor3", target: "actor0", value: 1 }, { source: "actor3", target: "actor1", value: 2 }, { source: "actor3", target: "actor2", value: 1 }, { source: "actor4", target: "actor0", value: 2 }, { source: "actor4", target: "actor2", value: 1 }, { source: "actor4", target: "actor3", value: 2 } ];
const result = Object.values(data.reduce((acc, { source, target, value }) => {
const key = [source, target].sort().join('-');
acc[key] = acc[key] || { source, target, value };
return acc;
}, {}))
console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; }