Simply create a map with name as key and use Object destructing
and Object.values()
to get the desired result.
const arr = [
{name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
{name: "survey with select", value: "survey with select answer", count: 2},
{name: "werasd", value: "Donald", count: 1},
{name: "werasd", value: "Jim", count: 1}
];
let result = Object.values(arr.reduce((a,{name, ...props})=>{
if(!a[name])
a[name] = Object.assign({}, {name,data : [props]});
else
a[name].data.push(props);
return a;
},{}));
console.log(result);