# \[M2-js\]How create native array and object?

**URL:** <https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149>\
**Category:** Uncategorized\
**Created:** [June 11, 2012, 2:16pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149 "2012-06-11T14:16:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dotneter](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dotneter](https://discuss.kotlinlang.org/u/dotneter)\
**Post date:** [June 11, 2012, 2:16pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/1 "2012-06-11T14:16:16Z")

</div>

var x = [1,2,3]; var x = {x:10}; ? Also, will labeled tuples be in version 1.0? If not, what can we use similar to json?

---

<div class="post-metadata">

**Author:** ![jstrachan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstrachan/32/1559_2.png) [@jstrachan](https://discuss.kotlinlang.org/u/jstrachan)\
**Post date:** [June 12, 2012, 10:45am UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/2 "2012-06-12T10:45:59Z")

</div>

You can create arrays and maps as follows; not sure if/when labelled tuples will be added.

> ```kotlin
> val x = array(1, 2, 3)
> val y = hashMap("a" to 3, "b" to 7)
> ```

`
`

---

<div class="post-metadata">

**Author:** ![dotneter](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dotneter](https://discuss.kotlinlang.org/u/dotneter)\
**Post date:** [June 12, 2012, 11:34am UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/3 "2012-06-12T11:34:18Z")

</div>

I download [https://github.com/abreslav/kotlin-js-hello](https://github.com/abreslav/kotlin-js-hello), open it, but kotlin-jslib.jar doesn't have array and hashMap. Version 0.1.2580

---

<div class="post-metadata">

**Author:** ![jstrachan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstrachan/32/1559_2.png) [@jstrachan](https://discuss.kotlinlang.org/u/jstrachan)\
**Post date:** [June 12, 2012, 12:05pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/4 "2012-06-12T12:05:51Z")

</div>

Ah sorry - I was talking JVM stuff; we're in the process of migrating the standard library over to JS; its going to take a few weeks to have those APIs available in JS.

---

<div class="post-metadata">

**Author:** ![dotneter](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dotneter](https://discuss.kotlinlang.org/u/dotneter)\
**Post date:** [June 12, 2012, 1:06pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/5 "2012-06-12T13:06:12Z")

</div>

But, it's not about standart library. array and hashMap return JVM classes. I guese I can write something like this

native(“Array”)  
class JsArray\<T\>() {  
&nbsp;&nbsp;fun push(item:T):Unit = js.noImpl  
&nbsp;&nbsp;fun pop() : T = js.noImpl  
&nbsp;&nbsp;fun get(index:Int):T = js.noImpl  
&nbsp;&nbsp;fun set(index : Int, value : T) : Unit = js.noImpl  
}

&nbsp;&nbsp;val x = JsArray\<Int\>()  
&nbsp;&nbsp;x.push(1)  
&nbsp;&nbsp;var z = x[0]

But it generate

&nbsp;&nbsp;var x = new Array;  
&nbsp;&nbsp;x.push(1);  
&nbsp;&nbsp;var z = x.get(0);

What I need to change to get?  
&nbsp;&nbsp;var x = new Array;  
&nbsp;&nbsp;x.push(1);  
&nbsp;&nbsp;var z = x[0];

---

<div class="post-metadata">

**Author:** ![jstrachan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstrachan/32/1559_2.png) [@jstrachan](https://discuss.kotlinlang.org/u/jstrachan)\
**Post date:** [June 12, 2012, 1:31pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/6 "2012-06-12T13:31:14Z")

</div>

There is already a kotlin standard library in JS, though its only got the basics of using kotlin code in JS so far; we're gradually migrating more of the [kotlin.\* APIs](http://jetbrains.github.com/kotlin/versions/snapshot/apidocs/index.html) over to work on JS.

We have a java.util.ArrayList compatible class already in JS for example. But more work is required to get the various [kotlin package APIs](http://jetbrains.github.com/kotlin/versions/snapshot/apidocs/kotlin/package-summary.html) working along with arrays and maps.

---

<div class="post-metadata">

**Author:** ![dotneter](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dotneter](https://discuss.kotlinlang.org/u/dotneter)\
**Post date:** [June 12, 2012, 1:58pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/7 "2012-06-12T13:58:13Z")

</div>

So, now there is not way to generate javascript square brackets? All I can do it's Array.prototype.get = function(x){ &nbsp;&nbsp;return this[x]; }

Object.prototype.get = function(x){  
&nbsp;&nbsp;return this;  
}

?

---

<div class="post-metadata">

**Author:** ![p.Talanov](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@p.Talanov](https://discuss.kotlinlang.org/u/p.Talanov)\
**Post date:** [June 13, 2012, 12:25pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/8 "2012-06-13T12:25:17Z")

</div>

Hi! Kotlin arrays are directly translated to JavaScript arrays. The difference is that Kotlin arrays are immutable. Also JavaScript - backend is lacking those nice constructor functions from stdlib like array(1, 2, 3) but they will be there soon.

As for native objects there is no equivalent on language level. You can use function js.json to create json object to use it with some native library. It is not translated directly to object literal but uses some ugly wrapper functions.  
Example:

```kotlin
val a = js.json(#(“1”, “value”), #(“2”, “value2”))
a[“3”] = “value3”
val c = a[“2”]
```

This example can be made prettier by introducing “to” function like this:

```kotlin
val a = js.json (
  “1” to “value”,
  “2” to “value2”
)
```

But still not as nice as the json notation.

Any suggestions are welcome.

---

<div class="post-metadata">

**Author:** ![dotneter](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dotneter](https://discuss.kotlinlang.org/u/dotneter)\
**Post date:** [June 13, 2012, 1:19pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/9 "2012-06-13T13:19:50Z")

</div>

\>Kotlin arrays are directly translated to JavaScript arrays. But, what if I want use native arrays, with push,pop api? [http://devnet.jetbrains.net/thread/436786?tstart=0](http://devnet.jetbrains.net/thread/436786?tstart=0) \>Any suggestions are welcome. May be add Symbols? val a = js.json ( &nbsp;&nbsp;'1 to "value", &nbsp;&nbsp;'2 to "value2" )

---

<div class="post-metadata">

**Author:** ![jhonny007](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@jhonny007](https://discuss.kotlinlang.org/u/jhonny007)\
**Post date:** [May 7, 2018, 7:33pm UTC](https://discuss.kotlinlang.org/t/m2-js-how-create-native-array-and-object/1149/10 "2018-05-07T19:33:14Z")

</div>

You can easily add the following extension functions in Kotlin JS:

```
fun <T> Array<T>.push(item: T) { asDynamic().push(item) }
fun <T> Array<T>.pop(): T = asDynamic().pop() as T

```
