multiply 2 value from each array of objects then add the multiplied numbers to get a total number in Reactjs/Javascript

I am building an eCommerce app where I want to get the total price of users ordered items. I’ve an array called ‘orders’ where I have all the ordered items. each items has 2 key calles payablePrice & purchasedQty. I want to multiply this 2 keys then i want to add the multiplied number to get the total.
Plz let me know if theres a better way to get the total,

Here is my array of objects:

orderarray

This is what i tried:

      {user.orders.map((order, i) => (
    <tr key={i}>
      <td> {order._id} </td>
      <td> {userName} </td>
      <td>
        {order.items.map((item) => item.payablePrice * item.purchasedQty)}
      </td>
      <td>status</td>
    </tr>
  ))}

which is not working.Plz help me get the total price of each order

The result i’m getting:
enter image description here

1 thought on “multiply 2 value from each array of objects then add the multiplied numbers to get a total number in Reactjs/Javascript”

  1. So, i just had to use .reduce after .map() in the correct way this code solved my problem

     {user.orders.map((order, i) => (
            <tr key={i}>
              <td> {order._id} </td>
              <td> {userName} </td>
              <td>
                {order.items
                  .map((item) => item.payablePrice * item.purchasedQty)
                  .reduce((acc, current) => {
                   return acc + current;
                  }, 0)}
              </td>
              <td>status</td>
            </tr>
          ))}
    
    Reply

Leave a Comment