# \[Extension\] BetterArrays - 60 useful array blocks!

**URL:** https://forum.makecode.com/t/extension-betterarrays-60-useful-array-blocks/29154
**Category:** Show & Tell
**Tags:** extension
**Created:** [June 7, 2024, 4:44pm UTC](https://forum.makecode.com/t/extension-betterarrays-60-useful-array-blocks/29154 "2024-06-07T16:44:30Z")
**Posts on this page:** 1
**Showing post:** 2

<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: [June 7, 2024, 7:06pm UTC](https://forum.makecode.com/t/extension-betterarrays-60-useful-array-blocks/29154/2 "2024-06-07T19:06:12Z")

</div>

> **Checks**
>
> The Checks category (new category) contains blocks that return true or false based on a condition inside of an array. BetterArrays adds **7** blocks to it.
> 
> > **Equal**
> >
> > The equal method returns true if two arrays are equal (regular javascript equality operation does not work on arrays, for some reason)
> > 
> > ```auto
> > function equal(first: any[], second: any[]): boolean
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example does **not** return `true`, because _javascript_.
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `true` (as it should)
> 
> > **In Range**
> >
> > The inRange method returns true if **index** is in range (between 0 and array length) of array.
> > 
> > ```auto
> > function inRange(array: any[], index: number): boolean
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `true` (0 \< 1 \< 2 is true)
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `false` (0 \< 2 \< 2 is false)
> > 
> > ### Exceptions
> > 
> > **NON\_INTEGER\_VALUE** if _index_ is not an integer
> 
> > **Includes**
> >
> > The includes method returns true if **item** is included in an array.
> > 
> > ```auto
> > function includes(array: any[], item: any): boolean
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `true`
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `false`
> 
> > **Is Empty**
> >
> > The isEmpty method returns true if an array is empty (length is 0).
> > 
> > ```auto
> > function isEmpty(array: any[]): boolean
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `true`
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `false`
> 
> > **All True**
> >
> > The allTrue method returns true if all elements in an array evaluate to true.
> > 
> > ```auto
> > function allTrue(array: any[]): boolean
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `true`
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `false` (0 evaluates to false)
> 
> > **Any True**
> >
> > The anyTrue method returns true if any element in an array evaluates to true.
> > 
> > ```auto
> > function anyTrue(array: any[]): boolean
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `true` (only true evaluates to true, but that’s enough to return true)
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `false` (all elements evaluates to false)
> 
> > **All Equal**
> >
> > The allEqual method returns true if every element in an array is equal to **item**.
> > 
> > ```auto
> > function allEqual(array: any[], item: any): boolean
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `true`
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `false`

> **Mutual operations**
>
> The Mutual Operations category (new category) contains blocks that perform operations comparing elements of two arrays and return the result. BetterArrays adds **3** blocks to it.
> 
> > **To Union**
> >
> > The toUnion method returns a union (all elements from both arrays together, no duplicates) of two arrays. This method does **not** modify the first array.
> > 
> > ```auto
> > function toUnion(first: any[], second: any[]): any[]
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `["foo", "bar", "baz"]`
> 
> > **To Intersection**
> >
> > The toIntersection method returns an intersection (only elements that appear in both arrays, no duplicates) of two arrays. This method does **not** modify the first array.
> > 
> > ```auto
> > function toIntersection(first: any[], second: any[]): any[]
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `["baz"]`
> 
> > **To Difference**
> >
> > The toDifference method returns a difference (elements that appear in the first array, without the items from the second array) of two arrays. This method does **not** modify the first array.
> > 
> > ```auto
> > function toDifference(first: any[], second: any[]): any[]
> > 
> > ```
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > ### Examples
> > 
> > ![image](https://sea2.discourse-cdn.com/flex020/images/transparent.png)
> > 
> > This example returns `["foo"]`

* * *

# Exceptions

As you can gather from the blocks chapter (see above), some blocks have an _exceptions_ category. This category describes which exceptions a block can throw, and under which condition that can occur.

BetterArrays throws exceptions for illegal values to make debugging easier and prevent unexpected behaviour. Below is an explanation of each of those exceptions.

> **NON\_INTEGER\_VALUE**
>
> This exception is thrown if a given **value** is not an integer (whole number)  
> Certain values (mostly indicies) cannot be decimal values
> 
> > Value must be integer (not [value])

> **NEGATIVE\_VALUE**
>
> This exception is thrown if a given **value** is negative  
> Certain values (mostly indicies) cannot be lower than 0
> 
> > Value must not be negative (not [value])

> **OUT\_OF\_RANGE**
>
> This exception is thrown if a given **value** is not within an **array** range  
> Values used for accessing values within an array must not be outside of array bounds
> 
> > Index ([value]) must be in list range (0, [array length; excluded])

> **ZERO\_STEP**
>
> This exception is thrown if a **step** value is equal to 0  
> Stepping values cannot be 0, as it would cause an infinite loop
> 
> > Stepping value cannot be 0

> **INVALID\_RANGE**
>
> This exception is thrown if an **end** value is lower than a **start** value  
> Start values must be lower than end values in order to create a valid range
> 
> > Start value ([start]) must be lower than end value ([end])

> **EMPTY\_ARRAY**
>
> This exception is thrown if a given **array** is empty  
> Certain methods require arrays containing at least one element
> 
> > Operation cannot be performed on empty array

> **NOT\_ARRAY**
>
> This exception is thrown if a given **value** type is not array  
> Certain methods expect elements to be an array type
> 
> > Expected array type (not [typeof value])

> **INVALID\_SHIFT**
>
> This exception is thrown if a **shift** value is bigger than an **array** length  
> The shift and toShifted methods can throw this exception
> 
> > Shift value ([shift]) cannot be bigger than array length ([length of array])

* * *

# Hardware Support

@danger_kitty pointed out that this extension (because it’s so huge) could take up too much device storage, making this extension unusable for projects that are intended for hardware ports.

After testing this theory, it was discovered that adding this extension to an empty project increases the compiled file size by a **whopping 0.4%**! Therefore, we can conclude that this extension can be used for hardware games as well.

* * *

And that’s the BetterArrays extension! I’ve sank a lot of my time making this extension, both writing code, and this post. If you find this useful, smash like and star the repo, would appreciate. If you found a mistake in this post, or unexpected behaviour in the extension, please let me know. Also be sure to submit any feature requests/ideas, as I’m open to adding new stuff!

Import the extension here

> **[GitHub - sargedev/betterarrays: Extension that adds many useful utility array...](https://github.com/sargedev/betterarrays)**
>
> Extension that adds many useful utility array methods

Now, I’m going to take a _very long_ nap. Peace ✌

---

_[View the full topic](https://forum.makecode.com/t/extension-betterarrays-60-useful-array-blocks/29154)._
