I have a table in frontend and need to put some data into the table. Below is what I am trying to achieve
____________________________________________________
| Product Name | Product Number | Product Location |
----------------------------------------------------
| A | 123 | TEXAS |
----------------------------------------------------
| B | 456 | NEW YORK |
----------------------------------------------------
However, I am keep getting like below table.
____________________________________________________
| Product Name | Product Number | Product Location |
----------------------------------------------------
| A | 123 | TEXAS |
NEW YORK
----------------------------------------------------
| B | 456 | TEXAS |
NEW YORK
----------------------------------------------------
Below is my code with JSX and render functions
const getTheLocation = () => {
return (
productsHistoryLocation.map((productHistory) => (
<p key={productHistory.product_id}>{productHistory.product_location}</p>
))
)
}
const renderProducts = () => {
return (
productsData.map((product) =>
(
<tr key={product.product_number}>
<td>{products.bacs_unit}</td>
<td>{products.serial_number}</td>
<td>{getTheLocation()}</td>
</tr>
))
)
}
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell><b>Product Name</b></TableCell>
<TableCell><b>Product Number</b></TableCell>
<TableCell><b>Product Location</b></TableCell>
</TableRow>
</TableHead>
<TableBody>
{renderProducts()}
</TableBody>
</Table>
getTheLocationmethod with some product-dependent variable. As per your code, it looks likegetTheLocationwill have elements for all theproductHistoryLocation. If you can add some input data, it would be more clear.renderProductsandgetTheLocationare behaving as independent functions and as you have two locations,renderProductsis runninggetTheLocationfor both of locations and thus the result. If the Location is somehow associated with the Product, you could pass in that associated index down to get location function and display only location associated to that particular product.