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 to check if value of a key exists of an array in another array

I have two arrays-

var arr1=  [
{
  VILLAGES_NAME: 'Chozuba Vill.',
  VI_ID: 269292,
  VI_SUBDIST_ID: 1806
},
{
  VILLAGES_NAME: 'Thevopisu',
  VI_ID: 269381,
  VI_SUBDIST_ID: 1806
},
{
  VILLAGES_NAME: 'Chozubasa (UR)',
  VI_ID: 269293,
  VI_SUBDIST_ID: 1806
}
  ];



let arr2=[
{
  LOCALITIES_NAME: 'CHOZUBA PART TWO',
  LOCALITY_ID: 301,
  VILLAGE_ID: 269292
},
{
  LOCALITIES_NAME: 'CHOZUBA PART ONE',
  LOCALITY_ID: 300,
  VILLAGE_ID: 269292
}
  ];

I want to check while looping arr1 if value of VI_ID of arr1 is matching anywhere in VILLAGE_ID of arr2.

This is what I have tried till now but not getting expected result.

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

 for(let i=0;i<arr1.length;i++)
  {
      if(arr2.includes(arr1[i].VI_ID))
      {
           console.log(arr1[i].VILLAGES_NAME)
      }
      
  }

>Solution :

Replace the condition with this one:

if (arr2.some(sth=> sth.VILLAGE_ID === arr1[i].VI_ID)) {
    ...
  }
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