DEV Community

Cover image for Navigating Arrays in JavaScript with is.array and is.not_array: Your Companion in List Adventures
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on

Navigating Arrays in JavaScript with is.array and is.not_array: Your Companion in List Adventures

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

The Marvel of Arrays in JavaScript

Documentation link

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:

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
Enter fullscreen mode Exit fullscreen mode

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:

import { is } from 'thiis';

const potentialArray = someFunctionThatMayReturnAnArray();

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

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:

import { is } from 'thiis';

const importantValue = someFunctionThatShouldNotBeAnArray();

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

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:

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.
}
Enter fullscreen mode Exit fullscreen mode

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:

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);
  });
Enter fullscreen mode Exit fullscreen mode

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:

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.
}
Enter fullscreen mode Exit fullscreen mode

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:

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.
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

I've refrained from commenting on your posts before, but this one really takes the cake...

Why on earth would you include a library to do something that is literally part of the JavaScript language? (Array.isArray)

Collapse
 
karbashevskyi profile image
Ivan Karbashevskyi

Hi, thanks for your question! While JavaScript does provide the Array.isArray method, the inclusion of these methods in the package allows for convenient combinations. For instance, you can create combinations like is.array_not_empty, is.array_or_string, is.array_empty, and more. This flexibility enables users to build custom combinations tailored to their specific needs. Feel free to use the methods that suit your requirements best! 😊

Collapse
 
karbashevskyi profile image
Ivan Karbashevskyi

Telegram channel:
t.me/thiis_pkg

NPM:
npmjs.com/thiis