Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

67
Views
create table with Object.entries

i'm pretty new with react and web dev in general.

I've written down this code but it doesn't work like I intended (classic)


 return (
 
 <div>
 
        <table className="table table-stripped">
                <thead>
                        <tr>
                                <th>Name</th>
                                <th>Profit in KRW</th>
                                <th>Profit %</th>
                        </tr>
                </thead>
                <tbody>
                        
                        {Object.entries(data).forEach(([key, value]) => {
                        return (
                                 <tr>
                                     <td>{key}</td>
                                     <td>{value['profit_krw']}</td>
                                     <td>{value['profit_perc']}</td>
                                 </tr>
                                );
                         })}
                        
                </tbody>
        </table>
     
</div>

)

Here data looks like {'ada':{'profit_krw': '5000', 'profit_perc': '0.2'}, 'btc': {'profit_krw': '10000', 'profit_perc': '0.4'}}

So in the end I would like it to look like ideally,

Name       Profit in KRW        Profit%
   ada             5000               0.2
   btc            10000               0.4
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Foreach doesn't return anything so you will need to use a map there or you will need to push onto an array. I would recommend the map:

   <tbody> 
    {Object.entries(data).map(([key, value]) => {
       return (
           <tr>
             <td>{key}</td>
             <td>{value['profit_krw']}</td>
             <td>{value['profit_perc']}</td>
           </tr>
          );
        })}
   </tbody>
about 4 years ago · Juan Pablo Isaza Report

0

In order to return something you can use map() because forEach() doesn't return anything

Here is a sandbox

export default function App() {
  const data = {
    ada: { profit_krw: "5000", profit_perc: "0.2" },
    btc: { profit_krw: "10000", profit_perc: "0.4" }
  };

  return (
    <div>
      <table className="table table-stripped">
        <thead>
          <tr>
            <th>Name</th>
            <th>Profit in KRW</th>
            <th>Profit %</th>
          </tr>
        </thead>
        <tbody>
          {Object.entries(data).map(([key, value]) => {
            return (
              <tr>
                <td>{key}</td>
                <td>{value["profit_krw"]}</td>
                <td>{value["profit_perc"]}</td>
              </tr>
            );
          })}
        </tbody>
      </table>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!