1

I have three arrays. One with static values, the other one has dynamic values, one array which will be filled with values that are the equal in the two arrays.

I would like to loop through the arrays and search for equal values. When a equal value has been found, this value should be put inside another array.

Something like this:

Array1 = ["Store1", "Store2", "Store3", "Store4"];
Array2 = ["Store6", "Store1", "Store3", "Store999"];
MatchedArray = ["Store1", "Store3"]; // should be filled with this

However, I don't like the idea of two for loops, like this:

  for(var arr1 = 0; arr1 < Array1.length; i++){
    for(var arr2 = 0; arr2 < Array2.length; i++){
      if(Array1[arr1].toLowerCase() == Array2[arr2].toLowerCase(){
        console.log('store found');
        duplicateArray.push(Array1[i].toLowerCase());
      }
    }
  }

I would like to know how I can use the .map or filter function or some other ways to accomplish this.

eisbehr
  • 12,243
  • 7
  • 38
  • 63
Elvira
  • 1,410
  • 5
  • 23
  • 50
  • Possible duplicate of [Simplest code for array intersection in javascript](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – user3297291 Apr 03 '18 at 10:43
  • You're right. Think this one can be closed due to duplication. – Elvira Apr 03 '18 at 10:55

6 Answers6

4

You can use a combination of Array#filter and Array#includes :

const arr1 = [ 'Store1', 'Store2', 'Store3', 'Store4'];
let arr2 = [ 'Store6', 'Store1', 'Store3', 'Store999'];

let res = arr2.filter(e => arr1.includes(e));
console.log(res);
Zenoo
  • 12,670
  • 4
  • 45
  • 69
2

Or make a generic array intersection function

 var intersection = function(){
    return Array.from(arguments).reduce(function(previous, current){
    return previous.filter(function(element){
      return current.indexOf(element) > -1;
   });
  });
 };

 var x = intersection([1,2,3],[2,3,4]);  // or pass n number of array as an argument
 console.log(x);
ashwintastic
  • 2,262
  • 3
  • 22
  • 49
1

Filter one of the arrays, and check if the other one contains the item using Array.includes():

var Array1 = [ 'Store1', 'Store2', 'Store3', 'Store4'];
var Array2 = [ 'Store6', 'Store1', 'Store3', 'Store999'];
var MatchedArray = Array1.filter(function(s) {
  return Array2.includes(s);
});

console.log(MatchedArray);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1
var Array1 = [ "Store1", "Store2", "Store3", "Store4"];
var Array2 = [ "Store6", "Store1", "Store3", "Store999"];

var Array3 = Array1 .filter(function(val) {
return Array2 .indexOf(val) != -1;
});

Array3
(2) ["Store1", "Store3"]
dchan
  • 363
  • 3
  • 8
0

You can use array.filter method to get the desired result,

var filtered = arr1.filter(function(val) {
    return arr2.indexOf(val) >= 0
})

This function is filtering array items based on the given condition. The condition here is I'm checking whether the val is present in arr2 or not.

Shreyas Kumar
  • 164
  • 1
  • 6
0

If you consider Underscore.js here is the way for such kind of operation, for any number of array

var Array1 = [ 'Store1', 'Store2', 'Store3', 'Store4'];
var Array2 = [ 'Store6', 'Store1', 'Store3', 'Store999'];
var Array3 = [ 'Store1', 'Store5', 'Store3', 'Store201'];
var common = _.intersection( Array1 ,Array2, Array3);
console.log(common);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109