DEV Community

MOHSIN ALI SOOMRO
MOHSIN ALI SOOMRO

Posted on

How to multiply two arrays in javascript

Multiply first element of array to the second element of array like [2*2] ,[4*4] , [4*4], [4*4] and so on...
example

const firstArray = [
  [2, 4, 4, 4],
  [3, 2, 2, 2],
  [1, 5, 9, 1],
  [5, 5, 5, 5]
];
const secondArray = [
  [2, 4, 4, 4],
  [3, 2, 2, 2],
  [1, 5, 9, 1],
  [5, 5, 5, 5]
];
Enter fullscreen mode Exit fullscreen mode

code

    firstArray.map((f, index) => {
      f.map((fs, idx) => {
        console.log(`${idx} = `, fs * secondArray[index][idx]);
      });
    });
Enter fullscreen mode Exit fullscreen mode

output

4
16
16
16
9
4
1
25
81
1
25
25
25
25
Enter fullscreen mode Exit fullscreen mode

Discussion (0)