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.
for(let i=0;i<arr1.length;i++)
{
if(arr2.includes(arr1[i].VI_ID))
{
console.log(arr1[i].VILLAGES_NAME)
}
}
2
Answers
You can’t do it with only one for loop without changing one of the arrays type. For example:
Replace the condition with this one: