Convert tibble/dataframe to JS array in R

Say you have the following data:


offset <- tribble(
  ~index, ~month,
  0, "Jul",
  1, "Aug",
  2, "Sep",
  3, "Oct"

How would one go about converting offset into the following JavaScript friendly array?

'offset': {
  0: 'Jul',
  1: 'Aug',
  2: 'Sep',
  3: 'Oct',

I’ve tried using library(jsonlite) via this post but it doesn’t provided the desired outcome:


#> [{"index":0,"month":"Jul"},{"index":1,"month":"Aug"},{"index":2,"month":"Sep"},{"index":3,"month":"Oct"}]

toJSON(offset, dataframe = "rows")
#> [{"index":0,"month":"Jul"},{"index":1,"month":"Aug"},{"index":2,"month":"Sep"},{"index":3,"month":"Oct"}]

toJSON(offset, dataframe = "columns")
#> {"index":[0,1,2,3],"month":["Jul","Aug","Sep","Oct"]}

toJSON(offset, dataframe = "values")
#> [[0,"Jul"],[1,"Aug"],[2,"Sep"],[3,"Oct"]]

My experience with JavaScript is laughably limited so any help would be much appreciated!

Created on 2021-03-25 by the reprex package (v1.0.0)


Is this near enough:

month <- offset$month
names(month) <- offset$index
cat(rjson::toJSON(list(offset = month)))