Navigating Arrays in JavaScript with is.array and is.not_array: Your Companion in List Adventures
Tags: beginners, webdev, codenewbie, coding
Published: 2023-11-19 12:04:03

JavaScript is like a vast playground filled with arrays – ordered lists of goodies waiting to be explored. But how can you be sure if something is an array or not? Fear not, for the `is.array` and `is.not_array` methods from the 'thiis' package are here to guide you through the twists and turns of your list adventures. In this article, we'll embark on a delightful journey to understand these tools and discover their magic in JavaScript.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7xijmngbl4oe06br344.jpeg)

## The Marvel of Arrays in JavaScript
[Documentation link](https://karbashevskyi.github.io/thiis/docs/API/Common/is.array)

Arrays are the superheroes of data storage in JavaScript. They can hold anything from numbers to strings, objects, or even other arrays. Recognizing them is fundamental for efficient data manipulation and iteration in your code.

## Meet `is.array` - Your Array Whisperer

Imagine you're in a vast library of data, and you want to make sure a certain variable is an array. The `is.array` method acts as your array whisperer, ensuring that a value is indeed an array. Let's dive into its charm:

```javascript
import { is } from 'thiis'; // Import the "is" object from the "thiis" package

const myArray = [1, 2, 3];
const result = is.array(myArray);

console.log(result); // true
```

In this example, we import the "is" object from the "thiis" package and use the `is.array` method to confirm that `myArray` is indeed an array. As expected, it returns `true` because the value is an array.

## The Journey of Examples

Now, let's embark on a journey through six practical examples that showcase the versatility of `is.array` and its sidekick, `is.not_array`. We'll explore different scenarios, from basic checks to more advanced ones.

### 1. **Basic Array Checking with `is.array`**

The primary role of `is.array` is to confirm that a value is indeed an array. It's your go-to tool when you want to make sure you're dealing with a list:

```javascript
import { is } from 'thiis';

const potentialArray = someFunctionThatMayReturnAnArray();

if (is.array(potentialArray)) {
  // Let the array adventures begin!
} else {
  // Handle other data types gracefully.
}
```

### 2. **Guarding Against Arrays with `is.not_array`**

Conversely, `is.not_array` is your guardian against arrays. It ensures that a value isn't an array before proceeding:

```javascript
import { is } from 'thiis';

const importantValue = someFunctionThatShouldNotBeAnArray();

if (is.not_array(importantValue)) {
  // Your guardian prevents array mishaps!
} else {
  // Time to explore other possibilities.
}
```

### 3. **Navigating Arrays with `forEach`**

When you're working with arrays and want to ensure you're dealing with one before using methods like `forEach`, `is.array` is your ally:

```javascript
import { is } from 'thiis';

const myDynamicData = getData(); // Assumes getData() returns an array or some other data type

if (is.array(myDynamicData)) {
  myDynamicData.forEach(item => {
    // Navigate the array with confidence!
    console.log(item);
  });
} else {
  // Handle the case where it's not an array.
}
```

### 4. **Stream of Arrays with `stream$`**

In the world of RxJS and observable streams, `is.array` can help ensure that you're dealing with an array before applying stream operations:

```javascript
import { is } from 'thiis';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

const stream$ = from([1, 2, 3, 'not an array', 4, 5, [], [1,2,3]]);

stream$
  .pipe(
    filter(is.array)
  )
  .subscribe(array => {
    // Only arrays will join the stream party!
    console.log(array);
  });
```

Here, `filter(is.array)` ensures that only arrays are processed in the stream.

### 5. **Array Transformation with `map`**

When transforming array elements using `map`, it's helpful to ensure the input is an array. `is.array` can come to the rescue:

```javascript
import { is } from 'thiis';

const myDynamicArray = getDynamicArray(); // Assumes getDynamicArray() returns an array or some other data type

if (is.array(myDynamicArray)) {
  const transformedArray = myDynamicArray.map(item => {
    // Transform array elements confidently!
    return item * 2;
  });
  console.log(transformedArray);
} else {
  // Handle the case where it's not an array.
}
```

### 6. **Array Composition with `concat`**

When combining arrays using `concat`, it's wise to confirm that both operands are arrays. `is.array` makes this a breeze:

```javascript
import { is } from 'thiis';

const array1 = [1, 2, 3];
const array2 = getDynamicArray(); // Assumes getDynamicArray() returns an array or some other data type

if (is.array(array2)) {
  const combinedArray = array1.concat(array2);
  console.log(combinedArray);
} else {
  // Handle the case where array2 is not an array.
}
```

## The Adventure Continues

The `is.array` and `is.not_array` methods from the 'thiis' package are your reliable companions on your JavaScript array adventures. They make array navigation playful and precise, ensuring your code interacts with arrays exactly as intended. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can master the art of handling lists with confidence and flair.

So, keep coding, and remember that arrays in JavaScript are like treasure chests waiting to be explored! 

> 🎗 ChatGPT & DALL·E 3