-2
     var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

1.)If elements are equals,show that common elemenst in output 2.)The output(common elements) should be array form

sam_programmer
  • 51
  • 2
  • 10

1 Answers1

2

Use Array#filter method and inside filter function use Array#indexOf or Array#includes methods to check second array includes the element.

var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var res = array1.filter(function(v) { // iterate over the array
  // check element present in the second array
  return array2.indexOf(v) > -1;
  // or array2.includes(v)
})

console.log(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188