У меня есть такой массив:
array = [
{
"id": 1,
"title": "Welcome to /r/artificial!",
"author": "CyberByte",
"ups": 128,
"comments": 16,
"created_at": "2017-06-19T20:16:35.000Z"
},
{
"id": 2,
"title": "Welcome",
"author": "Igor",
"ups": 12,
"comments": 06,
"created_at": "2017-06-19T20:16:35.000Z"
},
{
"id": 3,
"title": "Teste",
"author": "CyberByte",
"ups": 11,
"comments": 1,
"created_at": "2017-06-19T20:16:35.000Z"
},
]Я хочу сгруппировать их по авторам и подвести итоги и комментарии. Пытаюсь использовать reduce.
Как я могу добиться этого с помощью простого javascript?
Проверьте здесь: stackoverflow.com/questions/40774697/…
reduce - это простой JavaScript. пожалуйста, добавьте то, что вы пробовали.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Теперь он закрыт как дубликат, но у меня есть ответ, поэтому я решил, что могу его опубликовать:
const array = [
{
"id": 1,
"title": "Welcome to /r/artificial!",
"author": "CyberByte",
"ups": 128,
"comments": 16,
"created_at": "2017-06-19T20:16:35.000Z"
},
{
"id": 2,
"title": "Welcome",
"author": "Igor",
"ups": 12,
"comments": 6,
"created_at": "2017-06-19T20:16:35.000Z"
},
{
"id": 3,
"title": "Teste",
"author": "CyberByte",
"ups": 11,
"comments": 1,
"created_at": "2017-06-19T20:16:35.000Z"
},
]
const results = array.reduce((prev, curr) => ({
...prev,
[curr.author]: {
comments: (prev[curr.author] ? prev[curr.author].comments : 0) + curr.comments,
ups: (prev[curr.author] ? prev[curr.author].ups : 0) + curr.ups
}
}), {});
console.dir(results)
Не могли бы вы обновить вопрос, включив в него то, что вы пробовали до сих пор?