Reputation: 47
I am new to Javascript and am trying to get the below output from my_result object. I tried using map function but am unable to get the desired output.
const my_result = '[{"result":true, "count":42}, {"result":false, "count":30}]';
//Output
[ [ true, 42 ], [false, 30] ]
I found similar example here here but I am not able to figure out how I can perform similar operation for multiple row count. (Remove Column name from array in JSON)
my_result can have variable row count based on the table I am pulling the data from.
Upvotes: 0
Views: 1358
Reputation: 5853
const my_result = '[{"result":true, "count":42}, {"result":false, "count":30}]';
const res = JSON.parse(my_result).map(Object.values)
console.log(res)
Upvotes: 3
Reputation: 3241
Map works great.
const my_result = '[{"result":true, "count":42}, {"result":false, "count":30}]';
const formatted = JSON.parse(my_result).map(o => ({
[o.result]: o.count
}));
console.log(formatted);
Upvotes: -1