Skip to main content

Array

The Array module provides comprehensive array manipulation capabilities for Soul, including functional programming methods, searching, sorting, and mathematical operations. It offers a JavaScript-like API for working with arrays efficiently.

Adding Elements

push - Add elements to end

Add one or more elements to the end of an array:

unshift - Add elements to beginning

Add one or more elements to the beginning of an array:

insert - Insert element at specific index

Insert an element at a specific position:

Removing Elements

pop - Remove last element

Remove and return the last element:

shift - Remove first element

Remove and return the first element:

remove - Remove first occurrence of value

Remove the first occurrence of a specific value:

removeAt - Remove element at index

Remove element at a specific index:

clear - Remove all elements

Remove all elements from an array:

Accessing Elements

get - Get element at index

Get element at a specific index:

set - Set element at index

Set element at a specific index:

first - Get first element

Get the first element:

last - Get last element

Get the last element:

at - Get element with negative index support

Get element at index with negative index support:

Searching

indexOf - Find first index of value

Find the first index of a specific value:

lastIndexOf - Find last index of value

Find the last index of a specific value:

includes - Check if array contains value

Check if an array contains a specific value:

find - Find first element matching predicate

Find the first element that matches a predicate:

findIndex - Find index of first match

Find the index of the first element matching a predicate:

Transformation

map - Transform each element

Transform each element using a function:

filter - Keep elements matching predicate

Keep only elements that match a predicate:

reduce - Reduce to single value

Reduce array to a single value:

forEach - Execute function for each element

Execute a function for each element:

flatMap - Map and flatten result

Map each element and flatten the result:

Array Manipulation

slice - Extract portion of array

Extract a portion of an array:

splice - Remove/replace elements

Remove or replace elements at a specific position:

concat - Combine arrays

Combine multiple arrays:

join - Join elements as string

Join array elements into a string:

reverse - Reverse array in place

Reverse the order of elements:

sort - Sort array in place

Sort array elements:

shuffle - Randomly shuffle array

Randomly shuffle array elements:

unique - Remove duplicates

Remove duplicate values:

flatten - Flatten nested arrays

Flatten nested arrays:

chunk - Split into chunks

Split array into chunks of specified size:

zip - Combine arrays element-wise

Combine multiple arrays element-wise:

Testing

every - Test if all elements match predicate

Test if all elements match a predicate:

some - Test if any element matches predicate

Test if any element matches a predicate:

isEmpty - Check if array is empty

Check if an array is empty:

Utility Methods

length - Get array length

Get the length of an array:

copy - Create shallow copy

Create a shallow copy of an array:

fill - Fill array with value

Fill array with a specific value:

range - Create array of numbers

Create an array of numbers in a range:

Mathematical Operations

sum - Calculate sum of numbers

Calculate the sum of numeric elements:

min - Find minimum value

Find the minimum numeric value:

max - Find maximum value

Find the maximum numeric value:

average - Calculate average

Calculate the average of numeric elements:

Advanced Examples

Data Processing Pipeline

Functional Programming Example

Array Manipulation Workflow

Search and Filter Operations

Best Practices

  1. Use appropriate methods: Choose the right method for your use case
  2. Handle empty arrays: Always check for empty arrays when needed
  3. Immutable operations: Use methods that return new arrays for immutable operations
  4. Performance considerations: Be aware of O(n) operations on large arrays
  5. Function composition: Chain operations for cleaner code
The Array module provides a comprehensive set of tools for array manipulation in Soul, enabling both imperative and functional programming styles for working with collections of data.