Home > Software design >  Check if first array's item is in second array
Check if first array's item is in second array

Time:08-12

I am building a simple react app and I am kind of stuck on a problem.

What I am trying to do :-

I am trying to check if firstArray's (separate dict inside) item is in secondArray and when I run the loop then checking if is working fine But I am also trying to get item which is not in the array. But it is showing unexpected results in else statement.

I have tried many times with creating different array every time but it is showing same strange results in else statement.

App.js

class App extends React.Component {

   const firstArray = [
    {
      "name" : "Blog 1"
    },
    {
      "name" : "Blog 2"
    },
   ]   

   const secondArray = [
    {
      "name" : "Blog 1"
    },
    {
      "name" : "Blog 2"
    },
    {
      "name" : "Blog 3"
    },
    {
      "name" : "Blog 4"
    },
   ]

  for (var k = 0; k < firstArray.length; k  ) {
    for (var l = 0; l < secondArray.length; l  ) {
      if (secondArray[l].name === firstArray[k].name) {
        // Getting items which are in the same array
        console.log(secondArray[l].name)
      } 
      else {
        // Getting items which are not in array
        console.log(secondArray[l])
      }
    }
  }

   render() {
       return (
          <div>Check below</div>
       )
   }
}

When I try to run the above code then it is showing,

console.log. In if statement statement

{name: 'Blog 1'}
{name: 'Blog 2'}

console.log. In else statement statement It is showing strange results. And I am trying to get results which are not in the first array

{name: 'Blog 1'}
{name: 'Blog 2'}
{name: 'Blog 3'}
{name: 'Blog 1'}
{name: 'Blog 2'}
{name: 'Blog 3'}
{name: 'Blog 1'}
{name: 'Blog 2'}
{name: 'Blog 3'}
{name: 'Blog 1'}
{name: 'Blog 2'}
{name: 'Blog 3'}

I have no idea why it is showing that result but it is showing this for hours.

CodePudding user response:

To determine if the item in the 2nd array is in the 1st array or not you can use a flag. Set it false before inner for and then true if the item exists. Finally check it after inner for.

 for (var k = 0; k < secondArray.length; k  ) {
    var exists = false;
    for (var l = 0; l < firstArray.length; l  ) {
      if (secondArray[k].name === firstArray[l].name) {
        exists = true;
        break;
      } 
    }
    if (exists){
        console.log(secondArray[k].name   " exists");
    } else {
      console.log(secondArray[k].name   " doesn't exist");
    }
 }

CodePudding user response:

Your else statement will work every time when secondArray[k].name === firstArray[l].name is false. Actually you are not checking if secondArray[k] is in first array, but you are checking if it equals to every item in the first array or not.

If I got you right you are trying to do is this:

const commonElements = secondArray.filter(secondElement => 
    firstArray.some(firstElement => firstElement.name === secondElement.name)
)

As a result you will get all elements that appear in both firstArray and secondArray

CodePudding user response:

I'm not really sure what kind of result you are expecting. Here's what I can get.

const firstArray = [{name: "Blog 1"},{name: "Blog 2"}];
const secondArray = [{name: "Blog 1"},{name: "Blog 2"},{name: "Blog 3"},{name: "Blog 4"}];

secondArray.forEach(({ name: second}) => {
    const status = firstArray.some(({ name: first}) => first === second) 
      ? 'exists' 
      : 'does not exist';
      
    console.log(`${second} - ${status} in the first array`);
});
.as-console-wrapper { max-height: 100% !important; top: 0 }

  • Related