-6
I have this JSON and I must arrange it by data
:
{
"clients": [
{ "id": 1, "name": "Juca" },
{ "id": 2, "name": "Beto" }
],
"purchase": [
{ "client_id": 1, "data": "11/04/2021", "total": 100.0 },
{ "client_id": 2, "data": "11/03/2021", "total": 83.71 },
{ "client_id": 2, "data": "08/02/2021", "total": 52.48 },
{ "client_id": 1, "data": "20/04/2021", "total": 399.99 }
]
}
In case I have a file (main.js) that imports the file object . json and prints for each customer their data, their ordered purchases by date and the total in purchases.
This is the expected response:
Client 1 - Juca
Purchase on 04/11/2021 in total R$ 100.00
Purchase in 04/20/2021 in total R$ 399.99
Customer total: R$ 499.99
Client 2 - Beto
Purchase on 02/08/2021 in total R$ 52.48
Purchase on 03/11/2021 in total R$ 83.71
Customer total: R$ 136.19
I’ve tried to use sort()
string manipulation using the split('/')
but nothing returns me the expected answer.
Obs.: code execution must be done using Node --experimental-json-modeules main.js
Your question is a little ambiguous, I suggest [Edit] to put the expected result. The JSON format should remain the same, just by ordering the
purchase
forclient_id
and subsequentlydata
?– Rafael Tavares
I agree with @Rafaeltavares regarding this. I answered your question based on what I was able to extract (perhaps with a solution where you can try to apply in your project)
– gleisin-dev