how to push mutual array elements in an array nested loop javascript

Solutions on MaxInterview for how to push mutual array elements in an array nested loop javascript by the best coders in the world

showing results for - "how to push mutual array elements in an array nested loop javascript"
Sofia
25 Aug 2017
1let array1 = ['element1', 'element2', 'element3', 'element4'];
2let array2 = ['array2element', 'element2', 'element4'];
3let mutualArrayElements = [];
4
5for (let i = 0; i < array1.length; i++) {
6  for (let j = 0; j < array2.length; j++) {
7    if (array1[i] === array2[j]) {
8      mutualArrayElements.push(array1[i]);
9    }
10  }
11};
12// for nested loops