1
headers = ["no", "car","color","type"]

//json response
resp = [
    {"color":"green", "car":"bmw", "type": "suv", "no":"1"},
    {"color":"red", "car":"honda", "type": "sedan", "no":"2"},
    {"color":"blue", "car":"vw", "type": "truck", "no":"3"},
]

how can i rearrange the object key:val position based on headers array, so that i can render them in a table like so:

no car color type
1 bmw green suv
2 honda red sedan
3 vw blue truck

I need to use resp as below to render in a table:

resp.map((i,k) => (
  <tr key={k}> 
    <td>{i["no"]<td>
    <td>{i["car"]<td>
    <td>{i["color"]<td>
    <td>{i["type"]<td>
  <tr>
}))

3
  • 2
    Javascript objects don't have an "order" of keys so you can't "arrange" them. You need to come up with a different solution. Commented Feb 27, 2021 at 22:59
  • i've always rearrange them manually, but this time, there are >60 keys.. Commented Feb 27, 2021 at 23:01
  • resp.map( (i,k) => (<tr key={k}> <td>{i["no"]<td><td>{i["car"]<td><td>{i["color"]<td><td>{i["type"]<td><tr>})) literally hard-coded. as you can see the table, it won't be good for UX, if you dont rearrange the resp, if you just dynamically render them. Commented Feb 27, 2021 at 23:05

2 Answers 2

2

You dont need to 'order' the json for you to form the table. JavaScript doesn't guarantee order for objects, unlike arrays. You could use map on resp to form the table row based on the order of header list.

resp.map(data => {
  let row = '<tr>'
  headers.forEach(header => {
    row = row + '<td>' +data[header] + '</td>';
  });
  return row += '</tr>';
});

You just need to make sure, that the order of the items in headers is in the same order that you need.

Sign up to request clarification or add additional context in comments.

Comments

1

No need to do any arrangement as Object keys in JavaScript aren't guaranteed to follow an order. If you need data in a particular order, you should use array.

For your current problem, you can iterate the rows and show it in order set by headers:

const rows = [
  { color: 'green', car: 'bmw', type: 'suv', no: '1' },
  { color: 'red', car: 'honda', type: 'sedan', no: '2' },
  { color: 'blue', car: 'vw', type: 'truck', no: '3' },
]

const headers = ['no', 'car', 'color', 'type']

function App() {
  return (
    <table border={1}>
      <tr>
        {headers.map((header) => (
          <th>{header}</th>
        ))}
      </tr>
      {rows.map((row) => (
        <tr>
          {headers.map((header) => (
            <td>{row[header]}</td>
          ))}
        </tr>
      ))}
    </table>
  )
}

ReactDOM.render(<App/>,document.getElementById('mydiv'))
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>

<body>
  <div id="mydiv"></div>
</body>

Comments

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.