# How to find common elements in two arrays javascript?

1 vote
88 views

## Problem:

Hello good souls,

``````var myarr1 = [1, 2, 3, 4, 5];
var myarr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];``````

I have two simple arrays like above. I want to sort out the common values between those two arrays. So can you find common elements in two arrays javascript for me? Please keep the program as simple as possible that I can understand easily.

Thanks for your effort and valuable time buddy.

1 vote

## Solution:

You can use a function called \$.grep() to find out the common values between two arrays.

``````var myarr1 = [1, 2, 3, 4, 5];
var myarr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var common = \$.grep(myarr1, function(element) {
return \$.inArray(element, myarr2 ) !== -1;
});
console.log(common);``````

## Related questions

7 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
5 views
Problem: If you could lead me in the right way, I would appreciate it.
47 views
I have to work with comparision between objects of two arrays this is actually my University Assignment someone please help me
6 views
Question: Is there a way to return the difference between two arrays in JavaScript? For example: var a1 = ['a', 'b']; var a2 = ['a', 'b', 'c', 'd']; // need ["c", "d"]
8 views
Problem: How to compare two arrays for matches in javascript?
54 views
I am new to this. I was doing that but I got into some problems.
57 views
Problem : My function accepts the two arrays of integers and returns true if below conditions are met 1. Arrays have the same length and 2. Each m.element should be less than each n.element of the same index All my test cases are passing except when int[] m = {1, 2, 3} and int[] n = ... = (m[j] < n[j]); for (j = 0; j <= m.length - 1; j++) {} return (sameLength && digitDifference); }