尝试弄清如何提取表格的一部分。 (难以完全解释)

huangapple go评论56阅读模式
英文:

Trying to figure out how to Grab part of a table. (hard to explain fully)

问题

以下是您要翻译的部分:

所以,我试图做的是...最好是展示。
另外,这涉及到FiveM,我试图修改一个现有的资源。

BMProducts = {
{
["lostItems"] = {
[1] = { name = "weapon_shotgun", price = 1500, crypto = 2500, amount = 1 },
},
}
}

table.insert(BMProducts, {
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
})

Config.Products = BMProducts

而且我有另一个配置,我需要提取正确的表,但现在不太确定如何做到这一点。

["products"] = Config.Products["ballasItems"],

这是我有的,但它无法读取它,我认为这是我在调试时看到的情况,当插入到表中时,它会分配一个数字,即;

[1] = {lostitems = {... [2] = {ballasItems = {...

一个可以工作,但我的最终目标是使代码能够与表插入一起使用,就是这样

BMProducts = { "lostItems" = { [1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 }, } "ballasItems" = { [1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 }, } }

这适用于上面的配置,因为上面的方式不分配数字,也不插入表。有没有办法设置正确的Products表格的配置?当我尝试将表格插入时以及与["products"] = Config.Products["ballasItems"], 一起尝试时,它找不到表格,我认为这是因为表格格式与底部的代码块不同,所以我的主要问题是当有一个表格插入时如何使["products"] = Config.Products["ballasItems"], 与正确的表格匹配。

英文:

So, what i am trying to do is... well it's best to show.
Also, this is about FiveM and I'm trying to modify an existing resource.

BMProducts = {
{
["lostItems"] = {
[1] = { name = "weapon_shotgun", price = 1500, crypto = 2500, amount = 1 },
},
}
}

table.insert(BMProducts, {
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
})

Config.Products = BMProducts

And I have a another config, that I need to pull the Correct table, but now sure entirely how

["products"] =	Config.Products["ballasItems"],

Is what I have but it won't read it, due to what I assume is what i saw when debugging, that when inserting to the table, it assigns a number, ie;

[1] = {lostitems = {...
[2] = {ballasItems = {...

One that works, but what my ultimate goal is to make the code plug and play with the table inserts, is this

BMProducts = 
{
["lostItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
}

which works with the config above because the way just above does not assign numbers and not inserting into a table. Any ideas how i can go about setting that config for the correct Products table?

When i try it with the table insert, and with the

["products"] =	Config.Products["ballasItems"],

It can't find the table, which is due to what i assume the table format being different than what it was, which was the code block at the bottom

so my main thing, is to get

["products"] =	Config.Products["ballasItems"],

to = the correct table when there is a table insert.

答案1

得分: 0

If you don't want to table.insert, then don't table.insert:

BMProducts["ballasItems"] = {
    { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
}

Now I think you told to not modify the config, so another approach is to just use that additional array index when indexing:

["products"] = Config.Products[2]["ballasItems"]

Which obviously assumes that the config never changes and the entry with ballasItems is on index 2.

If you do not know the index, you may want to iterate over Config.Products and look for the right product.

for i,v in ipairs(Config.Products) do
    if v["ballasItems"] then

    end
end

You may also consider the case where two or more products match.

英文:

If you don't want to table.insert, then don't table.insert:

BMProducts["ballasItems"] = {
    { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
}

Now I think you told to not modify the config, so another approach is to just use that additional array index when indexing:

["products"] = Config.Products[2]["ballasItems"]

Which obviously assumes that the config never changes and the entry with ballasItems is on index 2.

If you do not know the index, you may want to iterate over Config.Products and look for the right product.

for i,v in ipairs(Config.Products) do
    if v["ballasItems"] then

    end
end

You may also consider the case where two or more products match.

huangapple
  • 本文由 发表于 2023年2月8日 15:52:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定