How can I iterate over objects?

How can I iterate over objects without Object.keys() or Object.entries() ?
6 Replies
OverHash
OverHash4y ago
pairs and ipairs?
PoppingPopper
PoppingPopperOP4y ago
@OverHash What does that look like in typescript? And is there a resource I can look at for this?
OverHash
OverHash4y ago
This is luau things
local myTable { name = "Joe", age = 20 }

for i, v in pairs(myTable) do
print(i, v)
end
local myTable { name = "Joe", age = 20 }

for i, v in pairs(myTable) do
print(i, v)
end
will output
name Joe
age 20
name Joe
age 20
In TypeScript it just looks like the TS equivalent,
for (const [k, v] of pairs(myTable)) {
print(k, v);
}
for (const [k, v] of pairs(myTable)) {
print(k, v);
}
PoppingPopper
PoppingPopperOP4y ago
Wonderful! How did you come to know this? Are there any docs? (This is exactly what I needed thank you, you answered my question) No way are you saying this is a convention in vanilla TS? Am I crazy
OverHash
OverHash4y ago
This is a luau thing as I said http://lua-users.org/wiki/ForTutorial see pairs and ipairs
PoppingPopper
PoppingPopperOP4y ago
Forgive me I don't see how you translated the luau into TS. Thanks for helping me!

Did you find this page helpful?