Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter out an array of object removing elements with same values

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading