Tencent / puerts

PUER(普洱) Typescript. Let's write your game in UE or Unity with TypeScript.
Other
4.92k stars 687 forks source link

Dictionary foreach #1759

Open Avatarchik opened 2 months ago

Avatarchik commented 2 months ago

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

Avatarchik commented 2 months ago

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

zombieyang commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

Well you can try the first example

Avatarchik commented 2 months ago

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

Avatarchik commented 1 month ago

Hi! can someone give an example of how I can read the Dictionary in JavaScript?