Skip to content

Instantly share code, notes, and snippets.

@Ni55aN
Created February 12, 2017 19:02
Show Gist options
  • Save Ni55aN/4baea617887b33db86192ae947ca43c9 to your computer and use it in GitHub Desktop.
Save Ni55aN/4baea617887b33db86192ae947ca43c9 to your computer and use it in GitHub Desktop.
This function converts Lua table to JS object using lua.vm.js
function table2js(t)
local type = type(t)
if type == "number" or type == "string" then
return t
end
if not (t[1] == nil) then -- check "t" is array
local arr = js.global:Array()
for k,v in pairs(t) do
arr[k-1] = table2js(v) -- k-1 - in js array start at 0, in lua - at 1
end
return arr
end
if type == "table" then
local obj = js.global:Object()
for k,v in pairs(t) do
obj[k] = table2js(v)
end
return obj
end
return nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment