DEV Community

MOHSIN ALI SOOMRO
MOHSIN ALI SOOMRO

Posted on

2 1

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

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay