0

I have data coming in from an api, it has a field called Amount, I have to display sum of all Amount.

This is how I am displaying all the amounts,

<div>
     {orders.map(({ id, Amount }) => {

        return (
          <div key={id}>
            <div>{Amount}</div>
          </div>
      )
      })}
 </div>
  • Does this answer your question? [How to sum the values of a JavaScript object?](https://stackoverflow.com/questions/16449295/how-to-sum-the-values-of-a-javascript-object) – Justinas Apr 02 '22 at 07:29

2 Answers2

0

This is one way... there several ways..

const count = () => {
    let total = 0;
    orders.map( amount => total = total + amount)
    console.log(total)
    return(
        <div>${total}</div>
    )
};
-1

I am just writing the pseudocode. If this is what you are asking for

const [sum,setSum]=useState(0);
<div>
     {orders.map(({ id, Amount }) => {
             setSum(sum+Amount)
        return (
          <div key={id}>
            <div>{Amount}</div>
          </div>
      )
      })}
  <div>Sum={sum}</div>
 </div>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '22 at 10:59