js array elem in another array code example
Example 1: javascript array includes another array
const array1= ["cheese", "dough", "sauce", "pepperoni"]
const array2= ["mozzarella", "peppers", "chicken", "cheese"]
const isIncluded = array1.some(value => array2.includes(value))
const values = array1.filter(value => array2.includes(value))
Example 2: return elements of an array from another array javascript
var toBeFiltered = [1,2,3,4,5,6];
var elements = [6,3,2];
var filteredElements = toBeFiltered.filter(
function(e) {
return this.indexOf(e) >= 0;
},
elements
);
console.log(toBeFiltered);
console.log(elements);
console.log(filteredElements);