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

How can compare and exclude arrays elements with other arrays elements in react native

I need to compare two arrays
here is one arrays
[…showid]
here is another arrays
[…swipeeachotheruserId]

in this arrays ,some id will contains
this ids will same but not all are same
[ 3, 5 , 7 , 10 ,14 ] [ 2, 5, 7, 1, 20, 14 ]

some elements will be same but not all elements same

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

logic is

showid===swipeeachotheruserId ? will exclude not same id and display here : show id

how can compare and exclude this arrays values

>Solution :

const showid = [ 3, 5, 7 , 10 ,14 ];
const swipeeachotheruserId = [ 2, 5, 7, 1, 20, 14 ];

const duplicates = showid.filter(value => swipeeachotheruserId.some(oneElement => oneElement === value));

const mergedArray = [ 
  ...showid, 
  ...swipeeachotheruserId.filter(value => !duplicates.some(oneDuplicate => oneDuplicate === value))
];

const arrayWithoutDuplicate = mergedArray.filter(oneElem => !duplicates.some(value => oneElem === value));

console.log(duplicates);
console.log(mergedArray);
console.log(arrayWithoutDuplicate);
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