0

I want to calculate totalSum. Api gives 'amount' rows, but I need to add them together and cant yet find correct function. I am still learning with reactjs.

const url = "http://127.0.0.1:8000/api/expense";
const TableCardList = () => {
    const [data, setData] = useState([]);
    const getData = async () => {
        const response = await fetch(url);
        const data = await response.json();
        setData(data);
        console.timeLog(data);
    };
    useEffect (() => {
        getData();
    }, []);

    let tableList = data.map((row) => {
        return (
            <TableCard
            key={row.id}
            id={row.id}
            created_at={row.created_at}
            title={row.title}
            category={row.category}
            amount={row.amount}
            />
        );
    });

    return(
        <div className="row">
            <table class="table">
            {tableList}
            <td><b className="number"> {totalSum} Eur</b></td>
            </table>
        </div>
    );
};
export default TableCardList;
3
  • use tableList.length Commented May 24, 2022 at 10:01
  • Use data.length Commented May 24, 2022 at 10:06
  • I understand that I need lenght of array, but I dont know how to formulate this function and where to place it. I spend way too much time on this...lol Commented May 24, 2022 at 10:14

1 Answer 1

1

Array.prototype.reduce can do the trick

const url = "http://127.0.0.1:8000/api/expense";
const TableCardList = () => {
  const [data, setData] = useState([]);
  const [totalSum, setTotalSum] = useState(0);

  useEffect(() => {
    const getData = async () => {
      const response = await fetch(url);
      const data = await response.json();
      setData(data);
      console.timeLog(data);
    };
    getData()
  }, []);

  useEffect(() => {
    const total = data.reduce((acc, row) => acc + row.amount, 0);
    setTotalSum(total)
  }, [data]);

  let tableList = data.map((row) => (
    <TableCard
      key={row.id}
      id={row.id}
      created_at={row.created_at}
      title={row.title}
      category={row.category}
      amount={row.amount}
    />
  ));

  return (
    <div className="row">
      <table class="table">
        {tableList}
        <td><b className="number"> {totalSum} Eur</b></td>
      </table>
    </div>
  );
};

export default TableCardList;
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this is close but yet smth is missing. {totalSum} outputs all of row.amount numbers inline but doesnt add them together. Output looks like this: 0221.56433.0033.00332.5542.00 Eur. I need to add them.
Maybe row.amount is in string format, try to add parseFloat to row.amount like this const total = data.reduce((acc, row) => acc + parseFloat(row.amount), 0);
Sadly cant leave positive feedback. Thank you, works like a charm! I changed datatype to float because I needed to add cents as well. Bravo!
Why can't you give positive feedback? you can still mark the question as answered to help others

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.