0

enter image description here

I have this interface and through this interface I display a set of values, and as it is clear, I have a “deduction” and the deduction is an array in which either one, two, three, or several values, and as it is clear from the table that most of the lines contain I have one Deduction value, but in the fourth line with the ID number eleven it contains two Deduction values, and as it is clear in this line that the interface’s shape has changed and become not beautiful, the question and the problem I have is how can I display the Deduction matrix in a better String (numbers behind each other with commas and without spaces) without the line becoming bad

   {n.deductions.map((deduction) => (
                      <TableCell
                        key={deduction.id}
                        className="p-4  md:p-16"
                        component="th"
                        scope="row"
                        align="center"
                      >
                        <span>£</span>
                        {deduction.amount}
                      </TableCell>
                    ))}

This is the deduction matrix, I take the quantity and display it

 "deductions": [
            {
                "id": 11,
                "receiptId": 11,
                "type": "loan",
                "amount": 7500,
                "reason": "You have took a holiday...",
                "createdAt": "2022-03-04T12:21:10.145Z",
                "updatedAt": "2022-03-04T12:21:10.145Z",
                "deletedAt": null
            },
            {
                "id": 12,
                "receiptId": 11,
                "type": "loan",
                "amount": 1000,
                "reason": "You have took a holiday...",
                "createdAt": "2022-03-04T12:21:10.145Z",
                "updatedAt": "2022-03-04T12:21:10.145Z",
                "deletedAt": null
            }
        ]
2
  • Well, it depends. Deduction.amount is an array of Strings? or is it an array of numbers? In any case, what you want is to convert the values to strings, and then in the template use something like this: {deduction.amount.join(', ')} This way, you are putting every element of the array in a comma separated string. Commented Mar 4, 2022 at 13:04
  • i edit my post @TAVentura Commented Mar 4, 2022 at 13:13

2 Answers 2

1

Well, in this case, you should retrieve the 'amount' value from an array of Objects. Try the following:

//in the template
{deduction.map(singleDeduction => singleDeduction.amount)}

The previous code will return an array of 'amount' values. Ussually, when printing arrays, javascript automatically converts the array to a comma separated string. If this doesnt happen, just add .join at the end, like this:

{deduction.map((singleDeduction) => singleDeduction.amount).join(', ')}

Also, if you want to add the '£' symbol to every deduction, do this:

{deduction.map((singleDeduction) => "£" + singleDeduction.amount).join(', ')}
Sign up to request clarification or add additional context in comments.

3 Comments

I've done it, but it has to be approved by community members, just replace your <span>£</span>{deduction.amount} for 'my' {deduction.map((singleDeduction) => "£" + singleDeduction.amount).join(', ')} to se if it works
i have just one problem, where can i put the key in this code? <TableCell // key={deduction.id} className="p-4 md:p-16" component="th" scope="row" align="center" > {"["} {n.deductions .map((singleDeduction) => "£" + singleDeduction.amount) .join(", ")} {"]"} </TableCell>
Based on my little knowledge about React, I think it's okay the way you're doing it. Why? Does it give any trouble to you?
0

To render the array in this form : [7500, 1000] inside the table cell try this:-

<TableCell
                        key={deduction.id}
                        className="p-4  md:p-16"
                        component="th"
                        scope="row"
                        align="center"
                      >
                        <span>£</span>
                        {deduction.amount[0] + "," + deduction.amount[1] }
                      </TableCell>

3 Comments

But it can be more than two values, how can I treat this case?
can u put the previous code?
yes it can be two values if deductedAmount is an array , which code ! ?

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.