1.0.6 • Published 3 years ago
swifty-array-helpers v1.0.6
Array Helpers for JavaScript
What is this?
A collection of functions for the Array.prototype
inspired by the methods that are available in Swift
Usage
Import
You need to import the methods first. Example:
// ES6 module
import 'swifty-array-helper/firstWhere'
// node.js
require('swifty-array-helper/firstWhere')
firstIndex
Finds the index of the first element matching the predicate. Example:
const array = [5, 3, 7, 12]
const index = array.firstIndex(x -> x <= 4) // 1
firstWhere
Finds the first element matching the predicate (similar to firstIndex
).
Example:
const array = [5, 3, 7, 12]
const firstElementMatchingPredicate = array.firstWhere(x -> x <= 4) // 3
filteredWhere
Filters the array based on the passed predicate Example:
const array = [5, 3, 7, 12]
const filteredArray = array.filteredWhere(x -> x <= 6) // [5, 3]
compactMap
Maps all values, filtering null
or undefined
values out
Example:
const array = [
{ name: 'Henrik' },
{ name: 'Henrik', lastName: 'Panhans' },
{ name: 'Henrik' },
{ name: 'Henrik' }
]
const lastNames = array.compactMap(value -> value.lastName) // ["Panhans"]