如何在 Lua 数组前面添加一个项目?

s̮̦*_*̥̳̼ 2 lua

如题。我正在使用 neovim 配置,该数组的内容将连接到要执行的命令中:

cmd = { 'mono', omnisharp_bin, '--languageserver', '--hostPID', tostring(vim.fn.getpid()) }
Run Code Online (Sandbox Code Playgroud)

喜欢这个,但想象一下 还'mono'没有被预先添加。

Jas*_*ijn 5

您可以使用table.insert

cmd = { omnisharp_bin, '--languageserver', '--hostPID', tostring(vim.fn.getpid()) }
table.insert(cmd, 1, 'mono')
-- cmd is now what you want
Run Code Online (Sandbox Code Playgroud)

您可以使用在位置table.insert(mytable, position, value)插入并将该位置之后的所有值移过来,或者使用在数组部分的末尾插入一个值。valuepositiontable.insert(table, value)