Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary foreach #1759

Open
Avatarchik opened this issue Jun 16, 2024 · 5 comments
Open

Dictionary foreach #1759

Avatarchik opened this issue Jun 16, 2024 · 5 comments

Comments

@Avatarchik
Copy link

How can I iterate through a Dictionary in JavaScript?
Analogue C#
foreach (KeyValuePair<string, object> n in dic)

@Avatarchik
Copy link
Author

Hi! Can someone help me? I tried different options but it didn’t work, sorting through the dictionary...

@zombieyang
Copy link
Contributor

https://github.com/Tencent/puerts/blob/master/unity/Assets/core/upm/Editor/Resources/puerts/templates/dts.tpl.mjs#L241
here is an example.

and I remember that some has submitted a PR to support for (var item of CSDic) #1555 . You can try

@Avatarchik
Copy link
Author

Hi! thanks, but I don't quite understand how to use it...
var dic= Engine.DatValue("dat") as CS.System.Collections.Generic.Dictionary$2<string, object>;

for (var item of dic){
console.log(item)
}

Error: TypeError: dicis not iterable

Try:

  for (const [key,value] of dic) {
  console.log(`map: ${key}: ${value}`) 
}

Error: Type 'KeyValuePair$2<string, object>' must have a 'Symbol.iterator' method that returns an iterator.

@zombieyang
Copy link
Contributor

Well you can try the first example

@Avatarchik
Copy link
Author

I don't quite understand how I should use this, can you help me?
https://github.com/Tencent/puerts/blob/master/unity/Assets/core/upm/Editor/Resources/puerts/templates/dts.tpl.mjs#L241

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants