0

I'm working with ReactTs since 3 months and now I'm trying to create a simple table to put some response results.

ReactTs Action file:

const response = await dropOffService
  .getDropOffOrders(
    page,
    count,
    filters,
    sorting,
  );
if (!dropOffOrdersResponse) return;
handleServerResponse(Action.LIST, { dropOffOrdersResponse }, dispatch);

the point is I would like to show the total of order pieces of every element of principal array. This information is individually available in response but I would like to add a new field in my response, a easy way to show this information in table column.

API Response:

const res = {
  "data": [ {
    "id": 1,
    "trackingId": "D-1",
    "documentLink": null,
    "createdAt": "2021-09-03T01:40:46.508Z",
    "originNode": {
      "id": 1,
      "name": "Principal Node"
    },
    "orders": [ {
      "id": 1,
      "trackingId": "1500912172021",
      "address": "Fictional Address 123",
      "zipCode": "5009",
      "province": "Cordova",
      "state": "Cordova",
      "generatedShipment": true,
      "shipper": {
        "id": 1,
        "name": "unknown Shipper",
        "province": "Cordova",
        "city": "Cordova",
        "address": "Fictional Address",
        "isCashOnDelivery": false,
        "numberOfDays": null,
        "paymentMethod": null,
        "fixedAmount": null,
        "piecePercentage": null,
        "email": null,
        "surplusAllowed": null
      },
      "pieces": [
        { "id": 1 },
        { "id": 4 },
        { "id": 3 },
        { "id": 2 }
      ]
    } ],
    "container": null
  } ],
  "count": 1,
  "total": 1,
  "page": 1,
  "pageCount": 1,
};

I would like to count records in "Pieces" field and then add the result in a new field named piecesTotal below to "container" field. How is it possible to do?

Expect result:

.
.
.
{
          "id": 2
        }
      ]
    }],
    "container": null,
    "PIECESTOTAL": 4
  ],
    "count": 1,
    "total": 1,
    "page": 1,
    "pageCount": 1 
}

I think this operation could be do in action response.

Thanks!!!

1
  • 1
    There is no JSON here and no need to tag react. It is plain JS object manipulation. What did you try? Commented Sep 4, 2021 at 18:29

1 Answer 1

1
data.forEach(element => {
 var totalCount = 0
    element.order.forEach(order => {
        totalCount += order.pieces.length
    });
  element.piecesTotal = totalCount
});
Sign up to request clarification or add additional context in comments.

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.