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.5k7 gold badges42 silver badges65 bronze badges
asked Apr 3, 2018 at 9:40
2

6 Answers 6

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

answered Apr 3, 2018 at 9:44
Sign up to request clarification or add additional context in comments.

Comments

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);
answered Apr 3, 2018 at 9:56

Comments

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

answered Apr 3, 2018 at 9:43

1 Comment

There are so many answers.. But I've chosen this one to test it out. :)
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"]
answered Apr 3, 2018 at 9:44

Comments

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.

answered Apr 3, 2018 at 9:50

Comments

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>

answered Apr 3, 2018 at 9:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.