I need to sort this array of objects, in the format that gives me reading for the development of the layout below:
I arrived at this code, but it’s still not enough for what I need, besides that I’m not using the best practices… here’s the code:
JavaScript
x
91
91
1
const testeDate = () => {
2
let dates = [];
3
balance.history.map((balanceItem) => {
4
yearkey = formatDate(balanceItem.created_at, 'yyyy');
5
6
if (dates.filter((e) => e.year === yearkey).length > 0) {
7
dates.balances.push(balanceItem);
8
console.log('nivel 1');
9
} else {
10
dates.push({ year: yearkey, balances: balanceItem });
11
console.log('nivel 2');
12
}
13
});
14
15
console.log(dates);
16
};
17
18
19
const [balance, setBalance] = useState({
20
title: 'Warrior of Wisdom',
21
contract_number: 11111,
22
updated_at: '2022-05-11T20:13:05.000000Z',
23
published_at: '2022-05-11T20:13:05.000000Z',
24
25
history: [
26
{
27
id: 1,
28
invoicing: 99999,
29
expense: 99999,
30
project_profit: 99999,
31
developer_profit: 99999,
32
publisher_profit: 99999,
33
created_at: '2021-05-11T20:13:05.000000Z',
34
},
35
{
36
id: 2,
37
invoicing: 99999,
38
expense: 99999,
39
project_profit: 99999,
40
developer_profit: 99999,
41
publisher_profit: 99999,
42
created_at: '2021-06-11T20:13:05.000000Z',
43
},
44
{
45
id: 3,
46
invoicing: 99999,
47
expense: 99999,
48
project_profit: 99999,
49
developer_profit: 99999,
50
publisher_profit: 99999,
51
created_at: '2021-07-11T20:13:05.000000Z',
52
},
53
{
54
id: 4,
55
invoicing: 99999,
56
expense: 99999,
57
project_profit: 99999,
58
developer_profit: 99999,
59
publisher_profit: 99999,
60
created_at: '2022-05-11T20:13:05.000000Z',
61
},
62
{
63
id: 5,
64
invoicing: 99999,
65
expense: 99999,
66
project_profit: 99999,
67
developer_profit: 99999,
68
publisher_profit: 99999,
69
created_at: '2019-05-11T20:13:05.000000Z',
70
},
71
{
72
id: 6,
73
invoicing: 99999,
74
expense: 99999,
75
project_profit: 99999,
76
developer_profit: 99999,
77
publisher_profit: 99999,
78
created_at: '2020-05-11T20:13:05.000000Z',
79
},
80
{
81
id: 7,
82
invoicing: 99999,
83
expense: 99999,
84
project_profit: 99999,
85
developer_profit: 99999,
86
publisher_profit: 99999,
87
created_at: '2022-06-11T20:13:05.000000Z',
88
},
89
],
90
});
91
Above also contains the array that I need to modify, I need it to look like the example below:
JavaScript
1
63
63
1
const arrayPerfect = [
2
{
3
year: 2021,
4
balances: [
5
{
6
id: 1,
7
invoicing: 99999,
8
expense: 99999,
9
project_profit: 99999,
10
developer_profit: 99999,
11
publisher_profit: 99999,
12
created_at: '2021-05-11T20:13:05.000000Z',
13
},
14
{
15
id: 2,
16
invoicing: 99999,
17
expense: 99999,
18
project_profit: 99999,
19
developer_profit: 99999,
20
publisher_profit: 99999,
21
created_at: '2021-06-11T20:13:05.000000Z',
22
},
23
{
24
id: 3,
25
invoicing: 99999,
26
expense: 99999,
27
project_profit: 99999,
28
developer_profit: 99999,
29
publisher_profit: 99999,
30
created_at: '2021-07-11T20:13:05.000000Z',
31
},
32
],
33
},
34
{
35
year: 2022,
36
balances: [
37
{
38
id: 4,
39
invoicing: 99999,
40
expense: 99999,
41
project_profit: 99999,
42
developer_profit: 99999,
43
publisher_profit: 99999,
44
created_at: '2022-05-11T20:13:05.000000Z',
45
},
46
],
47
},
48
{
49
year: 2019,
50
balances: [
51
{
52
id: 5,
53
invoicing: 99999,
54
expense: 99999,
55
project_profit: 99999,
56
developer_profit: 99999,
57
publisher_profit: 99999,
58
created_at: '2019-05-11T20:13:05.000000Z',
59
},
60
],
61
},
62
];
63
Advertisement
Answer
You can group the data using Array.prototype.reduce. Refer to snippet below:
JavaScript
1
14
14
1
const data = [{id:1,invoicing:99999,expense:99999,project_profit:99999,developer_profit:99999,publisher_profit:99999,created_at:"2021-05-11T20:13:05.000000Z"},{id:2,invoicing:99999,expense:99999,project_profit:99999,developer_profit:99999,publisher_profit:99999,created_at:"2021-06-11T20:13:05.000000Z"},{id:3,invoicing:99999,expense:99999,project_profit:99999,developer_profit:99999,publisher_profit:99999,created_at:"2021-07-11T20:13:05.000000Z"},{id:4,invoicing:99999,expense:99999,project_profit:99999,developer_profit:99999,publisher_profit:99999,created_at:"2022-05-11T20:13:05.000000Z"},{id:5,invoicing:99999,expense:99999,project_profit:99999,developer_profit:99999,publisher_profit:99999,created_at:"2019-05-11T20:13:05.000000Z"},{id:6,invoicing:99999,expense:99999,project_profit:99999,developer_profit:99999,publisher_profit:99999,created_at:"2020-05-11T20:13:05.000000Z"},{id:7,invoicing:99999,expense:99999,project_profit:99999,developer_profit:99999,publisher_profit:99999,created_at:"2022-06-11T20:13:05.000000Z"}];
2
3
const result = Object.values(
4
data.reduce((res, o) => {
5
const key = o.created_at.slice(0, 4);
6
if (!res[key]) {
7
res[key] = { year: key, balances: [] };
8
}
9
res[key].balances.push(o);
10
return res;
11
}, {})
12
);
13
14
console.log(result);