Skip to content

select

Filter and transform array elements based on conditions.

Syntax

typescript
select<T, R>(
  array: T[],
  transform: (item: T) => R,
  condition?: (item: T) => boolean
): R[]

Parameters

  • array (T[]): The array to process
  • transform ((item: T) => R): Transformation function
  • condition ((item: T) => boolean, optional): Filter condition

Return Value

  • R[]: Filtered and transformed array

Examples

Basic Usage

typescript
import { select } from 'radash'

const users = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 35 }
]

const names = select(users, user => user.name)
// ['Alice', 'Bob', 'Charlie']

With Conditional Filtering

typescript
import { select } from 'radash'

const users = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 35 },
  { name: 'Diana', age: 20 }
]

const adultNames = select(
  users,
  user => user.name,
  user => user.age >= 25
)
// ['Alice', 'Bob', 'Charlie']

Transform and Filter

typescript
import { select } from 'radash'

const products = [
  { name: 'Laptop', price: 999 },
  { name: 'Phone', price: 599 },
  { name: 'Book', price: 29 },
  { name: 'Tablet', price: 399 }
]

const expensiveProducts = select(
  products,
  product => ({ ...product, isExpensive: true }),
  product => product.price > 500
)
// [
//   { name: 'Laptop', price: 999, isExpensive: true },
//   { name: 'Phone', price: 599, isExpensive: true }
// ]

Handling Nested Objects

typescript
import { select } from 'radash'

const orders = [
  { id: 1, customer: { name: 'Alice', city: 'New York' } },
  { id: 2, customer: { name: 'Bob', city: 'Los Angeles' } },
  { id: 3, customer: { name: 'Charlie', city: 'New York' } }
]

const nyCustomers = select(
  orders,
  order => order.customer.name,
  order => order.customer.city === 'New York'
)
// ['Alice', 'Charlie']

Numerical Calculations

typescript
import { select } from 'radash'

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

const evenSquares = select(
  numbers,
  n => n * n,
  n => n % 2 === 0
)
// [4, 16, 36, 64, 100]

String Processing

typescript
import { select } from 'radash'

const words = ['hello', 'world', 'javascript', 'typescript', 'react']

const longWords = select(
  words,
  word => word.toUpperCase(),
  word => word.length > 5
)
// ['JAVASCRIPT', 'TYPESCRIPT']

Handling Dates

typescript
import { select } from 'radash'

const events = [
  { title: 'Meeting 1', date: new Date('2023-01-01') },
  { title: 'Meeting 2', date: new Date('2023-01-15') },
  { title: 'Meeting 3', date: new Date('2023-02-01') }
]

const thisMonthEvents = select(
  events,
  event => event.title,
  event => event.date.getMonth() === 0 // January
)
// ['Meeting 1', 'Meeting 2']

Complex Conditions

typescript
import { select } from 'radash'

const employees = [
  { name: 'Alice', age: 25, department: 'Engineering', salary: 80000 },
  { name: 'Bob', age: 30, department: 'Sales', salary: 60000 },
  { name: 'Charlie', age: 35, department: 'Engineering', salary: 90000 },
  { name: 'Diana', age: 28, department: 'Marketing', salary: 70000 }
]

const seniorEngineers = select(
  employees,
  emp => ({ ...emp, level: 'Senior' }),
  emp => emp.department === 'Engineering' && emp.age >= 30
)
// [
//   { name: 'Charlie', age: 35, department: 'Engineering', salary: 90000, level: 'Senior' }
// ]

Handling Null Values

typescript
import { select } from 'radash'

const items = [
  { name: 'Item 1', category: 'A' },
  { name: 'Item 2', category: null },
  { name: 'Item 3', category: 'B' },
  { name: 'Item 4', category: undefined }
]

const categorizedItems = select(
  items,
  item => item.name,
  item => item.category != null
)
// ['Item 1', 'Item 3']

Chaining Operations

typescript
import { select } from 'radash'

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// First filter even numbers, then convert to strings
const evenStrings = select(
  numbers,
  n => n.toString(),
  n => n % 2 === 0
)
// ['2', '4', '6', '8', '10']

// Then filter strings with length greater than 1
const longEvenStrings = select(
  evenStrings,
  s => s,
  s => s.length > 1
)
// ['10']

Notes

  1. Transform function: Must provide a transform function
  2. Condition function: The condition function is optional
  3. Performance: Time complexity is O(n)
  4. Type safety: Supports complete TypeScript type inference

Differences from Other Functions

  • select: Performs filtering and transformation simultaneously
  • map: Only performs transformation
  • filter: Only performs filtering
  • map + filter: Requires two iterations

Performance

  • Time Complexity: O(n)
  • Space Complexity: O(n)
  • Use Cases: Data transformation and filtering

Released under the MIT License.