vaibhav shah
vaibhav shah

Reputation: 5069

compare one value with each value of array which is inside another array

I have two arrays

var array1 = ['me','you','our'];
var array2 = ['us','they','all'];

I have another array

var arrayList = [array1, array2]

Now I have one value which I want to compare with each value of each array inside arrayList.

How can we do that?

Upvotes: 0

Views: 2500

Answers (3)

Prasath K
Prasath K

Reputation: 5018

Try this...

var yourValue;
for(var i=0;i<arrayList.length;i++)
{
   for(var j=0;j<arrayList[i].length;j++)
   {
      if(arrayList[i][j] == yourValue)
      {
         //
         //
      }
   }
}

Upvotes: 3

gpojd
gpojd

Reputation: 23065

Loop through arrayList then use indexOf.

var val = 'you';
for(var i = 0; i < arrayList.length; i++){
    if(arrayList[i].indexOf(val) !== -1){
        alert('match');
    }
}

Upvotes: 0

Sudip
Sudip

Reputation: 2051

var val='your value';
for(var i=0;i<arrayList.length;i++)
{
if(arrayList[i].indexOf(val)>-1){
// do something
// and break
}
}

Upvotes: 0

Related Questions