2

Hi I am developing one jquery application. I am trying to compare the two arrays. For example,

Firstarray=["Mike","Jack"];
SecondArray=["Mike","Jack","Andy","Cruz"];

Whenever we compare above two arrays I want to return the strings which exists in both arrays or which are common to both arrays!

I tried as below. This piece of code is not working.

for (var i = 0; i < Firstarray.length; i++) {
   for (var j = 0; j < SecondArray.length; j++) {
     if (Firstarray[i] == SecondArray[j]) {
        alert('found ' + SecondArray[j]);
        return;
     }
   }
}

Can anyone help me in this regards! Thank you very much.

6 Answers 6

3

You can use indexOf() function

Firstarray=["Mike","Jack"];
SecondArray=["Mike","Jack","Andy","Cruz"];
var result = new Array();
for (var i = 0; i < Firstarray.length; i++) {
  if(SecondArray.indexOf(Firstarray[i])>=0){
    result.push(Firstarray[i]);
  }
}
console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

Worked. Thanks a lot
Whenever i go to hit that button, it says you can accept answer in 2 minutes.
3

Here is a solution using Array.prototype.filter and Array.prototype.some along with some ES6 flavor thrown in - see demo below:

var firstArray=["Mike","Jack"];
var secondArray=["Mike","Jack","Andy","Cruz"];

var result = secondArray.filter(a => firstArray.some(b => a === b));

console.log(result);

Comments

2

check this How can I find matching values in two arrays?

Array.prototype.diff = function(arr2) {
var ret = [];
this.sort();
arr2.sort();
for(var i = 0; i < this.length; i += 1) {
    if(arr2.indexOf( this[i] ) > -1){
        ret.push( this[i] );
    }
}
return ret;
};

Comments

2

var FirstArray=["Mike","Jack"];
var SecondArray=["Mike","Jack","Andy","Cruz"];
var commonArray = Array();
var count=0;

for (var i=0; i<FirstArray.length; i++) {
  for (var j=0;j< SecondArray.length;j++) {
    if (FirstArray[i] == SecondArray[j]){
      commonArray[count]=FirstArray[i];
      count++;
    }
  }
}

console.log(commonArray);

Comments

2

Try changing few things in your code :

var Firstarray=["Mike","Jack"];
var SecondArray=["Mike","Jack","Andy","Cruz"];
var matchedData = [];


for (var i = 0; i < Firstarray.length; i++) {
                for (var j = 0; j < SecondArray.length; j++) {
                    if (Firstarray[i] == SecondArray[j]) {
                        matchedData.push(SecondArray[j]);

                    }
                }
            }
            alert(matchedData);

working fiddle : https://jsfiddle.net/o3brcsvw/

Comments

2

try this

var Firstarray=["Mike","Jack"];
var SecondArray=["Mike","Jack","Andy","Cruz"];
var matchedData = [];
for (var i = 0; i < Firstarray.length; i++) {
    for (var j = 0; j < SecondArray.length; j++) {
        if (Firstarray[i] == SecondArray[j]) {
            //alert('found ' + SecondArray[j]);
            matchedData.push(SecondArray[j]);
        }
    }
}
return matchedData;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.