What is the most efficient way to Group by and Convert an array into another JSON format in JS