JSFiddle - React, Tailwind, and code Playground
JavaScript
var responseData = [
{deviceType: "Smartphone", deviceCount: 14},
{deviceType: "Tablet", deviceCount: 11},
{deviceType: "Notebook", deviceCount: 3},
{deviceType: "Desktop", deviceCount: 2},
{deviceType: "Smartphone", deviceCount: 1},
{deviceType: "Tablet", deviceCount: 10},
{deviceType: "Notebook", deviceCount: 30},
{deviceType: "Desktop", deviceCount: 20}
];
function dataMapper(responseData){
var series = {};
if(responseData && responseData.length){
responseData.forEach(function(element){
series[element.deviceType] = Number.isInteger(series[element.deviceType]) ? series[element.deviceType] + element.deviceCount : element.deviceCount
});
}
var tmp = []
for (var i in series) {
tmp.push({deviceType: i, deviceCount: series[i]})
}
return tmp
}
console.log(dataMapper(responseData))