var arr = [
{
"name": "Daily poll",
"answers_count": 3
},
{
"name": "Daily poll",
"answers_count": 4
},
{
"name": "Daily poll",
"answers_count": 5
},
{
"name": "Sports poll",
"answers_count": 2
},
{
"name": "Sports poll",
"answers_count": 4
},
{
"name": "Sponsored poll",
"answers_count": 2
}
];
[
{Daily: [3, 4, 5]},
{Sports: [2,4]},
{Sponsored: [2]}
]
arr.reduce((results, poll) => {
const name = poll.name.split(' ')[0];
if (!results[name]) {
const tmp = {[name]: []};
results.push(tmp);
results[name] = tmp[name];
}
results[name].push(poll.answers_count);
return results;
}, []);
arr.reduce((results, poll) => {
const name = poll.name.split(' ')[0];
!results[name] && (results[name] = []);
results[name].push(poll.answers_count);
return results;
}, {});