# Use object as string map

**URL:** https://community.haxe.org/t/use-object-as-string-map/3784
**Category:** Haxe
**Tags:** haxe-js
**Created:** [December 4, 2022, 3:11pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784 "2022-12-04T15:11:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![neimanpinchas](https://community.haxe.org/user_avatar/community.haxe.org/neimanpinchas/32/2348_2.png) [@neimanpinchas](https://community.haxe.org/u/neimanpinchas)
#### Post date: [December 4, 2022, 3:11pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784/1 "2022-12-04T15:11:32Z")

</div>

I would like to create a function in Haxe, to be utilized, from (existing) javascript and php.

In JavaScript I was used to use a Plain object as a map (before es5 Maps), in haxe I would probably need to use Map\<String,T\> for this behavior.

The problem is that wen compiling into javascript it compiling to a special class having an internal h parameter holding the elements.

What is the preferred method to make a haxe function that could convert a plain js object, or php assoc array, into a string map?

---

<div class="post-metadata">

### Author: ![Ilir-Liburn](https://avatars.discourse-cdn.com/v2/letter/i/82dd89/32.png) [@Ilir-Liburn](https://community.haxe.org/u/Ilir-Liburn)
#### Post date: [December 4, 2022, 11:22pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784/2 "2022-12-04T23:22:17Z")

</div>

Hello,

there is a object map available in Haxe

> **[haxe.ds.ObjectMap - Haxe 4.2.1 API](https://api.haxe.org/haxe/ds/ObjectMap.html)**
>
> ObjectMap allows mapping of object keys to arbitrary values.

---

<div class="post-metadata">

### Author: ![joshtynjala](https://community.haxe.org/user_avatar/community.haxe.org/joshtynjala/32/792_2.png) [@joshtynjala](https://community.haxe.org/u/joshtynjala)
#### Post date: [December 5, 2022, 4:31pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784/3 "2022-12-05T16:31:53Z")

</div>

While a Map is probably a better choice in many cases, Haxe also supports [anonymous structures](https://haxe.org/manual/types-anonymous-structure.html) that work similarly to JS plain objects. If you type the variable as `Dynamic`, it basically works the same as JS. If you want the compiler to provide a little more protection for you, you can create a [typedef](https://haxe.org/manual/type-system-typedef.html) to specify which fields are available.

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [December 5, 2022, 5:04pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784/4 "2022-12-05T17:04:08Z")

</div>

The problem with typedef is that it generates unwanted fields in the generated object. At least in the Lua target

```haxe
typedef X = {
	var cmd:String;
}

```

```lua
_hx_o({ __fields__ ={cmd=true},cmd="Yo"})

```

---

<div class="post-metadata">

### Author: ![joshtynjala](https://community.haxe.org/user_avatar/community.haxe.org/joshtynjala/32/792_2.png) [@joshtynjala](https://community.haxe.org/u/joshtynjala)
#### Post date: [December 5, 2022, 5:23pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784/5 "2022-12-05T17:23:08Z")

</div>

Does it generate unwanted fields if you use `Dynamic` instead?

```haxe
var myObj:Dynamic = {};
myObj.cmd = "Yo";

```

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [December 5, 2022, 7:10pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784/6 "2022-12-05T19:10:33Z")

</div>

Yes, because they are created with this function:

```haxe
local function _hx_e()
  return setmetatable({ __fields__ = {}}, _hx_obj_mt)
end

```

---

<div class="post-metadata">

### Author: ![neimanpinchas](https://community.haxe.org/user_avatar/community.haxe.org/neimanpinchas/32/2348_2.png) [@neimanpinchas](https://community.haxe.org/u/neimanpinchas)
#### Post date: [December 22, 2022, 3:58pm UTC](https://community.haxe.org/t/use-object-as-string-map/3784/7 "2022-12-22T15:58:58Z")

</div>

Could I make an anonymous structure that is also itirable for optional fields?
