# Anyone know how to join two arrays together?

**URL:** https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660
**Category:** Arcade
**Created:** [January 29, 2023, 11:56pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660 "2023-01-29T23:56:20Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)
#### Post date: [January 29, 2023, 11:56pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/1 "2023-01-29T23:56:20Z")

</div>

I need to join multiple arrays together as one array. I can make an array with both arrays as its contents, but thats not what I need. Anyone know?

---

<div class="post-metadata">

### Author: ![Salex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/salex/32/15835_2.png) [@Salex](https://forum.makecode.com/u/Salex)
#### Post date: [January 30, 2023, 5:25pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/2 "2023-01-30T17:25:56Z")

</div>

Hi

If you mean adding one of the contents to the end of another array, I’d do something like: (also I write this in python sorry)

```auto
def arrayCombine(x, y):
    result = [] 
    for i in x:
        result.append(i)
    for i in y:
        result.append(i)
    return result

```

I’m doing this away from the editor, I cannot guarantee the example will work in a literal sense, but I’d do that in some shape or form

---

<div class="post-metadata">

### Author: ![MakeCode](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@MakeCode](https://forum.makecode.com/u/MakeCode)
#### Post date: [January 30, 2023, 5:29pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/3 "2023-01-30T17:29:04Z")

</div>

Hi!  
I just wanted to share a different approach to this! In python, you can actually just use the plus operator to join two lists together!

So, if you have list x and y, you can get the result list by doing: `result = x + y` !

It can be nice to find solutions where loops are not necessary.

---

<div class="post-metadata">

### Author: ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)
#### Post date: [January 30, 2023, 5:30pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/4 "2023-01-30T17:30:00Z")

</div>

Yeah thats about my current solution but in blocks form, using for loops through every value of each array. Gets quite big when joining all 9 arrays- But thanks!

---

<div class="post-metadata">

### Author: ![Salex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/salex/32/15835_2.png) [@Salex](https://forum.makecode.com/u/Salex)
#### Post date: [January 30, 2023, 5:30pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/5 "2023-01-30T17:30:10Z")

</div>

![Screenshot 2023-01-30 at 17.27.11](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/1fd7bac144f6db0f2e593be06979b08c1915a35f.png)  
never seen that before… just popped up now

---

<div class="post-metadata">

### Author: ![NobodyNomad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nobodynomad/32/10821_2.png) [@NobodyNomad](https://forum.makecode.com/u/NobodyNomad)
#### Post date: [January 30, 2023, 5:31pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/6 "2023-01-30T17:31:00Z")

</div>

i might be able to help i have something like that that a friend shared. give me a week or so to find it and potentially modify it/test it if it even is relevant and useful to this

---

<div class="post-metadata">

### Author: ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)
#### Post date: [January 30, 2023, 5:31pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/7 "2023-01-30T17:31:44Z")

</div>

Hehehe I can watch you type 👀

---

<div class="post-metadata">

### Author: ![Salex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/salex/32/15835_2.png) [@Salex](https://forum.makecode.com/u/Salex)
#### Post date: [January 30, 2023, 5:31pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/8 "2023-01-30T17:31:51Z")

</div>

oh right! I forgot this was a feasble solution; I like doing loops like this, idk why (i never make my code readable lol), but efficiency seems best here

---

<div class="post-metadata">

### Author: ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)
#### Post date: [January 30, 2023, 5:31pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/9 "2023-01-30T17:31:55Z")

</div>

Ah I didn’t know that, thanks so much!

---

<div class="post-metadata">

### Author: ![MakeCode](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@MakeCode](https://forum.makecode.com/u/MakeCode)
#### Post date: [January 30, 2023, 5:34pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/10 "2023-01-30T17:34:23Z")

</div>

Of course!

I’m not sure of the greatest blocks solution for this, but will update you if I find something helpful!

---

<div class="post-metadata">

### Author: ![Salex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/salex/32/15835_2.png) [@Salex](https://forum.makecode.com/u/Salex)
#### Post date: [January 30, 2023, 5:34pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/11 "2023-01-30T17:34:42Z")

</div>

seeing as moderation is in place (delaying time between post and view), why does this feature (actually seeing whos typing in real time) actually exist?

---

<div class="post-metadata">

### Author: ![MakeCode](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@MakeCode](https://forum.makecode.com/u/MakeCode)
#### Post date: [January 30, 2023, 5:39pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/12 "2023-01-30T17:39:02Z")

</div>

Just a feature of the platform. Even if you might not see the responding post right away, it might also be nice to know that someone is at least looking at your post and that you will have an answer soon.

---

<div class="post-metadata">

### Author: ![Salex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/salex/32/15835_2.png) [@Salex](https://forum.makecode.com/u/Salex)
#### Post date: [January 30, 2023, 5:43pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/13 "2023-01-30T17:43:11Z")

</div>

oh cool

---

<div class="post-metadata">

### Author: ![Salex](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/salex/32/15835_2.png) [@Salex](https://forum.makecode.com/u/Salex)
#### Post date: [January 30, 2023, 5:43pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/14 "2023-01-30T17:43:19Z")

</div>

see it in slomo debug mode (_evil laugh_)

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [February 1, 2023, 7:04pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/15 "2023-02-01T19:04:02Z")

</div>

I know it’s kind of late, but JavaScript has a convenient `Array.prototype.concat()` method. It returns a value without modifying the original array.  
Given `array1 = [1, 2] & array2 = [3, 4]`:

```auto
let array3 = array1.concat(array2);

```

‘array3’ will be defined as [1, 2, 3, 4]  
(this method is stackable, so you can do `array1.concat(array2).concat(array3)...`

* * *

In blocks you need to write your own method, as one is not in place. You could implement it something like this (pseudocode) :

```auto
function add_arrays(array1, array2)
    define new_array = array1
    for item in array do
        new_array.push(item)
    return new_array

```

Or alternatively (blocks) :

 ![example](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/5864094dda2702dccf5a39e42f6e8850ceda36f5.png)

---

<div class="post-metadata">

### Author: ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)
#### Post date: [February 1, 2023, 10:40pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/16 "2023-02-01T22:40:14Z")

</div>

Ah thanks! I’m much more fluent in JavaScript than Python so I’ll probably use that- Thanks for showing me!!

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [February 1, 2023, 10:41pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/17 "2023-02-01T22:41:36Z")

</div>

If you wanted to make a function that takes in any number of arrays to be concatenated, you could make use of the previously shown custom method to use it in a loop.  
(items is an array of arrays which are to be concatenated)

```auto
function add_many(items)
    define new_array = []
    if items.length <= 1 do
        return items
    for array in items do
        new_array = add_arrays(new_array, array)
    return new_array

```

In this method, I continuously update the ‘new\_array’ variable by concatenating the next array to it.

* * *

And the blocks implementation:

 ![example](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/e/e0252fb5d005adb3cc2039f04bb6737a723a2fa7.png)

Now, the blocks are complaining about type annotations - because TS really likes types (who could have known). The thing is that you can’t define types in blocks, so I kinda have no idea how to get around this. Even if I manually add them in the JS editor, the changes revert when I go back to blocks. The only other option I see at this point is if I just make a quick extension (if I can find some time for it) to concatenate which handles all the code and logic in JS anyways. For now your best bet is to just use my first method as many times as you have arrays (inconvenient but it’s the best we have rn)…  
@richard, is there a way to circumvent this in blocks, or am I stuck?

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [February 2, 2023, 6:11pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/18 "2023-02-02T18:11:57Z")

</div>

yeah, unfortunately this does not work in blocks because of how our type inference works. generic types do not have the best support right now. can you give me a link to your code, though @Sarge? i would love to have it as a test case if we ever go improve this.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [February 2, 2023, 6:17pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/19 "2023-02-02T18:17:44Z")

</div>

Of course! Here’s the code above

> **[concat](https://arcade.makecode.com/S52000-10871-59406-53008)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [February 2, 2023, 7:52pm UTC](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/20 "2023-02-02T19:52:56Z")

</div>

> [@Sarge](#):
>
> The only other option I see at this point is if I just make a quick extension

So, about that…

> **[GitHub - sargedev/array-methods: A MakeCode project](https://github.com/sargedev/array-methods)**
>
> A MakeCode project

Yeah, I did it anyway

[Next page](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660.md?page=2)
