For example, if I have 2 arrays being
['dog', 'cat', 'zebra', 'lion', 'cat']
and
['dog', 'cat', 'cat', 'frog']
then a new array should be created that only contains
['dog', 'cat', 'cat']
var array1 = ['dog', 'cat', 'zebra', 'lion', 'cat'];
var array2 = ['dog', 'cat', 'cat', 'frog'];
// Then you can use filter
// const filteredArray = array1.filter(value => array2.includes(value));
// (OR) `indexOf` for old browsers compability
var filteredArray = array1.filter(function(n) {
return array2.indexOf(n) !== -1;
});
console.log(filteredArray);