Javascript Array Group By Function In R