如何减少数组获取所需对象的总和?

时间:2018-09-20 15:47:56

标签: javascript arrays

我有大量的对象,需要对所需的键进行求和。 我尝试使用reduce来执行此操作,但是无法使其正常工作。 即使我尝试使用acc[item.agent_state] = acc + (+item.duration)

,返回值也始终是单一值
const states = _.uniqBy(data, function (e) {
  return e.agent_state;
});

const mappedStates = states.map(item => item.agent_state)

// const mappedStates = ['Logged-in']

const sumValues = data.filter(item => {
  return mappedStates.includes(item.agent_state)
})



const result = sumValues.reduce((acc, item, index, array) => {
    acc[item.agent_state] = +item.duration
    return acc

}, {})

Fiddle

2 个答案:

答案 0 :(得分:1)

如评论中所述,您应该使用+=进行递增。每次使用acc[item.agent_state] = +item.duration都只需重新分配属性。

在尝试向其中添加任何内容之前,您需要确保将每个键的acc对象初始化为零。否则,您将得到NaN,因为您正尝试添加到未定义的值。

例如:

const sumValues = [
  {agent_state: "state1", duration: 1},
  {agent_state: "state2", duration: 1},
  {agent_state: "state1", duration: 1},
  {agent_state: "state1", duration: 1},
  {agent_state: "state2", duration: 5},
  {agent_state: "state1", duration: 10},
  {agent_state: "state3", duration: 0}
]

const result = sumValues.reduce((acc, item, index, array) => {
  // iniliaze to 0 before trying to add things
  if (!acc.hasOwnProperty(item.agent_state)) acc[item.agent_state] = 0
  acc[item.agent_state] += item.duration
  return acc

}, {})
console.log(result)

答案 1 :(得分:0)

这是reduce的有效示例。您的数据集很大,因此我修剪了一些条目。完整数据集的输出应为:

Logged-in: 343435,
Logout   : 0,
Not Ready: 169516,
Ready    : 6632,
Reserved : 28,
Talking  : 373

const data = [{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:20:51.835","agent_state":"Logged-in","reason_code":"0","duration":"343434","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:20:51.837","agent_state":"Not Ready","reason_code":"32760","duration":"354","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:26:45.583","agent_state":"Logout   ","reason_code":"32765","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:29:05.848","agent_state":"Logged-in","reason_code":"0","duration":"1","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:29:05.85","agent_state":"Not Ready","reason_code":"32760","duration":"14","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:29:19.266","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:29:19.395","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:29:19.397","agent_state":"Not Ready","reason_code":"32760","duration":"192","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:32:31.087","agent_state":"Ready    ","reason_code":"0","duration":"159","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:35:10.197","agent_state":"Not Ready","reason_code":"202","duration":"233","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:39:03.256","agent_state":"Ready    ","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:39:03.324","agent_state":"Reserved ","reason_code":"0","duration":"4","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:39:07.374","agent_state":"Talking  ","reason_code":"0","duration":"10","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:39:17.926","agent_state":"Ready    ","reason_code":"0","duration":"63","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 12:40:20.621","agent_state":"Not Ready","reason_code":"203","duration":"7692","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:48:32.163","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:48:32.204","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:48:32.206","agent_state":"Not Ready","reason_code":"32760","duration":"7","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:48:39.89","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:48:39.919","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:48:39.921","agent_state":"Not Ready","reason_code":"32760","duration":"58","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:49:37.717","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:49:37.724","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:49:37.726","agent_state":"Not Ready","reason_code":"32760","duration":"113","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-10 14:51:30.083","agent_state":"Logout   ","reason_code":"301","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 07:49:18.497","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 07:49:18.499","agent_state":"Not Ready","reason_code":"32760","duration":"630","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 07:59:48.843","agent_state":"Not Ready","reason_code":"291","duration":"546","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:08:54.387","agent_state":"Ready    ","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:08:54.431","agent_state":"Reserved ","reason_code":"0","duration":"3","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:08:57.196","agent_state":"Talking  ","reason_code":"0","duration":"11","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:09:08.556","agent_state":"Ready    ","reason_code":"0","duration":"205","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:12:33.319","agent_state":"Not Ready","reason_code":"202","duration":"2","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:12:35.539","agent_state":"Logout   ","reason_code":"301","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:29:04.43","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:29:04.432","agent_state":"Not Ready","reason_code":"32760","duration":"203","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:32:27.479","agent_state":"Ready    ","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:32:27.483","agent_state":"Reserved ","reason_code":"0","duration":"3","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:32:30.517","agent_state":"Talking  ","reason_code":"0","duration":"46","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:33:16.89","agent_state":"Ready    ","reason_code":"0","duration":"5","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:33:21.128","agent_state":"Not Ready","reason_code":"203","duration":"1","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 08:33:22.954","agent_state":"Logout   ","reason_code":"301","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:01:52.55","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:01:52.552","agent_state":"Not Ready","reason_code":"32760","duration":"12","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:02:04.533","agent_state":"Ready    ","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:02:04.539","agent_state":"Reserved ","reason_code":"0","duration":"2","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:02:06.953","agent_state":"Talking  ","reason_code":"0","duration":"14","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:02:20.869","agent_state":"Ready    ","reason_code":"0","duration":"1558","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:28:18.779","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:28:18.796","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:28:18.798","agent_state":"Not Ready","reason_code":"32760","duration":"176","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:31:14.83","agent_state":"Not Ready","reason_code":"203","duration":"3","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 09:31:17.495","agent_state":"Logout   ","reason_code":"301","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 15:24:08.601","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 15:24:08.603","agent_state":"Not Ready","reason_code":"32760","duration":"56","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 15:25:04.811","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 15:25:04.818","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 15:25:04.82","agent_state":"Not Ready","reason_code":"32760","duration":"17","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-11 15:25:21.241","agent_state":"Logout   ","reason_code":"301","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:03:09.286","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:03:09.288","agent_state":"Not Ready","reason_code":"32760","duration":"793","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:16:22.448","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:16:22.456","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:16:22.458","agent_state":"Not Ready","reason_code":"32760","duration":"1293","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:37:55.307","agent_state":"Ready    ","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:37:55.757","agent_state":"Reserved ","reason_code":"0","duration":"4","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:37:59.811","agent_state":"Talking  ","reason_code":"0","duration":"65","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:39:04.645","agent_state":"Ready    ","reason_code":"0","duration":"1175","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:58:39.363","agent_state":"Not Ready","reason_code":"32759","duration":"24","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:59:03.1","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:59:11.749","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:59:11.751","agent_state":"Not Ready","reason_code":"32760","duration":"3","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:59:14.703","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:59:14.726","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 20:59:14.728","agent_state":"Not Ready","reason_code":"32760","duration":"383","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-12 21:05:37.889","agent_state":"Logout   ","reason_code":"255","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:17:32.096","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:17:32.098","agent_state":"Not Ready","reason_code":"32760","duration":"2","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:17:34.79","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:17:34.805","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:17:34.807","agent_state":"Not Ready","reason_code":"32760","duration":"303","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:22:37.337","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:22:37.341","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:22:37.343","agent_state":"Not Ready","reason_code":"32760","duration":"18","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:22:55.936","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:22:55.947","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:22:55.949","agent_state":"Not Ready","reason_code":"32760","duration":"18","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:23:13.052","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:23:13.056","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:23:13.058","agent_state":"Not Ready","reason_code":"32760","duration":"382","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 04:29:35.968","agent_state":"Logout   ","reason_code":"255","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:21:09.471","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:21:09.473","agent_state":"Not Ready","reason_code":"32760","duration":"65","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:22:14.085","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:22:14.086","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:22:14.087","agent_state":"Not Ready","reason_code":"32760","duration":"5","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:22:19.229","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:22:19.229","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:22:19.231","agent_state":"Not Ready","reason_code":"32760","duration":"2140","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:57:59.273","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:57:59.273","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 15:57:59.275","agent_state":"Not Ready","reason_code":"32760","duration":"682","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:09:21.186","agent_state":"Logout   ","reason_code":"255","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:09:29.148","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:09:29.15","agent_state":"Not Ready","reason_code":"32760","duration":"1390","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:32:39.109","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:32:39.109","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:32:39.111","agent_state":"Not Ready","reason_code":"32760","duration":"677","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:43:56.001","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:43:56.001","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:43:56.003","agent_state":"Not Ready","reason_code":"32760","duration":"12","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:44:08.948","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:44:08.948","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:44:08.95","agent_state":"Not Ready","reason_code":"32760","duration":"381","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:50:29.92","agent_state":"Logout   ","reason_code":"255","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:50:39.622","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 16:50:39.624","agent_state":"Not Ready","reason_code":"32760","duration":"2782","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 17:37:01.713","agent_state":"Logout   ","reason_code":"255","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 17:37:08.956","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Bla McElroy","agent_login_id":"agent4502","agent_extension":"4502","transition_time":"2018-09-13 17:37:08.958","agent_state":"Not Ready","reason_code":"32760","duration":"1526","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:46:03.543","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:46:03.556","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:46:03.558","agent_state":"Not Ready","reason_code":"32760","duration":"56","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:46:59.411","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:46:59.433","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:46:59.435","agent_state":"Not Ready","reason_code":"32760","duration":"117","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:48:56.621","agent_state":"Logout   ","reason_code":"255","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:50:19.904","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:50:19.906","agent_state":"Not Ready","reason_code":"32760","duration":"233","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:54:12.879","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:54:12.888","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:54:12.89","agent_state":"Not Ready","reason_code":"32760","duration":"43","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:54:55.197","agent_state":"Logout   ","reason_code":"32767","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:54:55.206","agent_state":"Logged-in","reason_code":"0","duration":"0","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:54:55.208","agent_state":"Not Ready","reason_code":"32760","duration":"89","latestsynchedtime":null},{"agent_name":"Richard Simental","agent_login_id":"agent4918","agent_extension":"4918","transition_time":"2018-09-11 13:56:24.253","agent_state":"Logout   ","reason_code":"255","duration":"0","latestsynchedtime":null}]

const result = data.reduce((a, e) => {
  if (!(e.agent_state in a)) {
    a[e.agent_state] = 0;
  }

  a[e.agent_state] += (+e.duration);  
  return a;
}, {});


console.log(result)