#javascript #node.js #arrays #lodash
#javascript #node.js #массивы #Lodash
Вопрос:
У меня есть массив объектов json в виде
[
{
"businessId": "7ab43023-7f40-40cf-b97c-563223bb27ef",
"id": "38fe3c68-e6aa-4c57-b4d7-dc6c4f597269",
"journalDate": "2020-08-13T00:00:00.000Z",
"transactionId": "146",
"accountId": "4",
"amount": 85,
"isReconciled": 0,
"active": 1,
"createdAt": "2020-08-14T02:55:43.988Z",
"updatedAt": "2020-08-14T02:55:43.988Z"
},
{
"id": "45bf4792-c5a5-44ed-b7e8-57557c4f30ee",
"journalDate": "2020-08-13T00:00:00.000Z",
"transactionId": "160",
"accountId": "4",
"amount": 70,
"isReconciled": 0,
"active": 1,
"createdAt": "2020-08-14T02:55:43.988Z",
"updatedAt": "2020-08-14T02:55:43.988Z"
},
{
"id": "5fe82eb0-17cc-4a08-97cf-0291b4b2b740",
"journalDate": "2020-08-13T00:00:00.000Z",
"transactionId": "158",
"accountId": "4",
"amount": 274.5,
"isReconciled": 0,
"active": 1,
"createdAt": "2020-08-14T02:55:43.988Z",
"updatedAt": "2020-08-14T02:55:43.988Z"
},
{
"id": "6690f228-35c1-4ba7-a0ff-a3e6a64cbc88",
"journalDate": "2020-06-30T00:00:00.000Z",
"transactionId": "151",
"accountId": "4",
"amount": -100,
"isReconciled": 0,
"active": 1,
"createdAt": "2020-08-14T02:55:43.988Z",
"updatedAt": "2020-08-14T02:55:43.988Z"
},
{
"id": "89a0e960-943d-4f0a-a81c-44d1ec27de59",
"journalDate": "2020-05-31T00:00:00.000Z",
"transactionId": "153",
"accountId": "4",
"amount": -60,
"isReconciled": 0,
"active": 1,
"createdAt": "2020-08-14T02:55:43.988Z",
"updatedAt": "2020-08-14T02:55:43.988Z"
}
]
Теперь я хочу сгруппировать данные для одного и того же идентификатора учетной записи за год. Например
{
"accountId": "4",
"Jan": [
{
"businessId": "7ab43023-7f40-40cf-b97c-563223bb27ef",
"id": "38fe3c68-e6aa-4c57-b4d7-dc6c4f597269",
"journalDate": "2020-01-13T00:00:00.000Z",
"transactionId": "146",
"accountId": "4",
"amount": 85,
"isReconciled": 0,
"active": 1,
"createdAt": "2020-08-14T02:55:43.988Z",
"updatedAt": "2020-08-14T02:55:43.988Z"
}
],
"Feb": [
{
"businessId": "7ab43023-7f40-40cf-b97c-563223bb27ef",
"id": "38fe3c68-e6aa-4c57-b4d7-dc6c4f597269",
"journalDate": "2020-02-13T00:00:00.000Z",
"transactionId": "146",
"accountId": "4",
"amount": 85,
"isReconciled": 0,
"active": 1,
"createdAt": "2020-08-14T02:55:43.988Z",
"updatedAt": "2020-08-14T02:55:43.988Z"
}
]
}
Пожалуйста, подскажите мне, как этого добиться?
Ответ №1:
Используйте forEach
поверх элементов и создайте объект с ключами и идентификатором учетной записи (имейте отдельные сегменты для каждого месяца). Получить Object.values
из только что созданного объекта.
const transform = (arr) => {
const all = {};
arr.forEach(({ accountId, journalDate, ...rest }) => {
if (!all[accountId]) {
all[accountId] = { accountId };
}
const month = new Date(journalDate)
.toDateString()
.split(" ")[1];
if (!all[accountId][month]) {
all[accountId][month] = [];
}
all[accountId][month].push({ accountId, journalDate, ...rest });
});
return Object.values(all);
};
const data = [
{
businessId: "7ab43023-7f40-40cf-b97c-563223bb27ef",
id: "38fe3c68-e6aa-4c57-b4d7-dc6c4f597269",
journalDate: "2020-08-13T00:00:00.000Z",
transactionId: "146",
accountId: "4",
amount: 85,
isReconciled: 0,
active: 1,
createdAt: "2020-08-14T02:55:43.988Z",
updatedAt: "2020-08-14T02:55:43.988Z",
},
{
id: "45bf4792-c5a5-44ed-b7e8-57557c4f30ee",
journalDate: "2020-08-13T00:00:00.000Z",
transactionId: "160",
accountId: "4",
amount: 70,
isReconciled: 0,
active: 1,
createdAt: "2020-08-14T02:55:43.988Z",
updatedAt: "2020-08-14T02:55:43.988Z",
},
{
id: "5fe82eb0-17cc-4a08-97cf-0291b4b2b740",
journalDate: "2020-08-13T00:00:00.000Z",
transactionId: "158",
accountId: "4",
amount: 274.5,
isReconciled: 0,
active: 1,
createdAt: "2020-08-14T02:55:43.988Z",
updatedAt: "2020-08-14T02:55:43.988Z",
},
{
id: "6690f228-35c1-4ba7-a0ff-a3e6a64cbc88",
journalDate: "2020-06-30T00:00:00.000Z",
transactionId: "151",
accountId: "4",
amount: -100,
isReconciled: 0,
active: 1,
createdAt: "2020-08-14T02:55:43.988Z",
updatedAt: "2020-08-14T02:55:43.988Z",
},
{
id: "89a0e960-943d-4f0a-a81c-44d1ec27de59",
journalDate: "2020-05-31T00:00:00.000Z",
transactionId: "153",
accountId: "4",
amount: -60,
isReconciled: 0,
active: 1,
createdAt: "2020-08-14T02:55:43.988Z",
updatedAt: "2020-08-14T02:55:43.988Z",
},
];
console.log(transform(data));