Find name of a value in a table?

Title may be unclear but I’ll try to explain what I mean.

local fruitTable = {
	Apple = {
		Price = 600,
		Description = "good"
	}
	,
	Watermelon = {
		Price = 500,
		Description = "good"
	}
	,
	Orange = {
		Price = 121,
		Description = "good"
	}
}

Essentially, I have this table; and I’m going to be looping through each individual value, in the parent table to find the values for each item in this large parent table. I want to try and figure out how to get the name of the value returned from the loop in say for example the Orange table.

Like, Price = 121; and I want to find out the name of this value; as it’ll just return “121” if I print it out.

TLDR: Is there any way to find the value name of an value in a table?

Table is not real I’m just using it as a demonstration cause I don’t really have anything better to use as a demonstration.

The pairs iterator function passes both the name and values for each table item.

So in this example you could go:

for fruitName, fruitData in pairs(fruitTable) do
	print(fruitName..":")
	for attribute, value in pairs(fruitData) do
		print("\t"..attribute..": "..value)
	end
end
3 Likes