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.
-
Possible duplicate of Simplest code for array intersection in javascriptuser3297291– user32972912018年04月03日 10:43:33 +00:00Commented Apr 3, 2018 at 10:43
-
You're right. Think this one can be closed due to duplication.Elvira– Elvira2018年04月03日 10:55:40 +00:00Commented Apr 3, 2018 at 10:55
6 Answers 6
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);
Comments
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);
Comments
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);
1 Comment
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"]
Comments
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.
Comments
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>