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

Javascript : Check array element contains element from another array

I have below array –

Array(12)
[
{username:"abc" , userpid:"M123"},
{username:"xyz" , userpid:"T234"},
{username:"mnp" , userpid:"L678"}
.
.
]

I have another array as –

Array (6)
    [
    {projectname:"corporate" , projecttype:"oil" userpid:"M123"},
    {projectname:"corporate" , projecttype:"oil" userpid:"K123"},
    {projectname:"corporate" , projecttype:"oil" userpid:"P123"},
    .
    .
    ]

Here , I wanted to filter out all the elements from first array whose userpid is not in second array. Eg. userpid M123 is present in second array thats why output –

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

[
{username:"xyz" , userpid:"T234"},
{username:"mnp" , userpid:"L678"}
]

I tried with - 

array1.some(x=>x.userpid!=(array2.filter(y=>y.userpid)))

But this is giving syntax error.

>Solution :

Something like this

const arr1 = [
{username:"abc" , userpid:"M123"},
{username:"xyz" , userpid:"T234"},
{username:"mnp" , userpid:"L678"}];

const arr2 = [
    {projectname:"corporate", projecttype:"oil", userpid:"M123"},
    {projectname:"corporate", projecttype:"oil", userpid:"K123"},
    {projectname:"corporate", projecttype:"oil", userpid:"P123"},];

const result = arr1.filter(item => !arr2.some(v => item.userpid === v.userpid));

console.log(result);
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