• Register
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.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

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);

This program might help you. Keep posting, Man

13 9 6
94,260 points

Related questions

0 votes
1 answer 7 views
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
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 5 views
5 views
Problem: If you could lead me in the right way, I would appreciate it.
asked Apr 11 salim1 60.6k points
0 votes
1 answer 47 views
47 views
I have to work with comparision between objects of two arrays this is actually my University Assignment someone please help me
asked Nov 10, 2020 Han Li 710 points
0 votes
1 answer 6 views
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"]
asked Apr 20 munim01 21k points
0 votes
1 answer 8 views
8 views
Problem: How to compare two arrays for matches in javascript?
asked Mar 20 Ifra 37.2k points
0 votes
1 answer 54 views
54 views
I am new to this. I was doing that but I got into some problems.
asked Nov 3, 2020 TeamScript 13.5k points
0 votes
1 answer 57 views
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); }
asked Dec 3, 2019 alecxe 7.5k points
0 votes
1 answer 24 views
24 views
I am trying to solve a "practice" problem, and I am not able to pass all the tests for it. The problem is: Write a method called equals that takes two arrays of strings and returns true if they are equal; that is, if both sets are the same length and contain ... { result = true; } else { result = false; } } } else { return false; } return result; } Expected performance: falseMy return:true
asked Oct 19, 2020 sasha 16.2k points
0 votes
1 answer 18 views
18 views
Problem: I need someone&rsquo;s assistance to solve my problem ..how to add array elements in javascript
asked May 2 Ifra 37.2k points
0 votes
1 answer 29 views
29 views
I want to know how to get multiple checked checkbox value in Jquery.
asked Oct 5, 2020 Daniel Anderson 4k points