-1

I have two arrays want to get index of each item in array2 like below:

const array1 = ['apple', 'orange', 'banana', 'mango', 'watermelon'];
const array2 = ['apple', 'orange', 'watermelon'];

const x = array2.map(item2 => array1.findIndex(item => item === item2 ));
console.log(Math.max(x)); // output: NaN

And then I am trying to get max number from x array but it output returned NaN. How can I get max number in this array?

Epple
  • 640
  • 14

1 Answers1

0

Try using the spread operator

const array1 = ['apple', 'orange', 'banana', 'mango', 'watermelon'];
const array2 = ['apple', 'orange', 'watermelon'];

const x = array2.map(item2 => array1.findIndex(item => item === item2))

console.log(Math.max(...x))

This should do the trick because it will take the array elements and pass them as parameters to Math.max()