This is the first time I am working with javascript. I have the following string:
document.write(str)
[{"company":1,"number_employees":5},
{"company":4,"number_employees":25},
{"company":5,"number_employees":5},
{"company":2,"number_employees":5},
{"company":4,"number_employees":25}]
I need to convert this string such that I summarize them according to the number_employees as follows:
Three companies have number_employees=5 and two companies have number_employees=25
I am currently struggling to convert the string into javascript object. Once I have the asscoiative array, I can convert it into map and count the number_employees
.
s_obj = eval('({' + messageBody + '})');
var result = s_obj.reduce(function(map, obj) {
map[obj.key] = obj.val;
return map;
}, {});
This is giving me the following error:
VM181:1 Uncaught SyntaxError: Unexpected token ,
at onMessage ((index):41)
at WebSocket.<anonymous> (stomp.min.js:8)
Any help/hint/guidance would be much appreciated!