More Collection Operations

Unless stated otherwise, all the following clone the input collection and do not modify it.

array.filter(function)

  • array is an array with elements of type T
  • function is a function:
    • Parameter 1: Element of type T
    • Parameter 2 (Optional): Index of array (0, 1, 2...) of type number
    • Returns a boolean
  • returns: an array with elements of type T

.filter( calls the function once for each element in the array. Each call passes the current element in as a parameter to the function.

If the function returns true the element is kept in the output array, otherwise it's discarded.

The output of the function is the original array with 0 or more elements missing.

lodash: _.filter(array, function)

Examples

[1, 2, 3].filter(x => x < 2)
// => [1]
[1, 2, 3].filter((x, index) => index < 2)
// => [1, 2]
// the second parameter is the current index of the element `x`
// in this example index would be: 0 then 1 then 2.
const arrayOfObjects = [{a: 1, b: 'green'}, {a: 5, b: 'red'}, {a: 3, b: 'blue'}]
arrayOfObjects.filter(object => object.a > 2)
// [{a: 5, b: 'red'}, {a: 3, b: 'blue'}]

array.map(function)

  • array is an array with elements of type T
  • function is a function:
    • Parameter 1: Element of type T
    • Parameter 2 (Optional): Index of array (0, 1, 2...) of type number
    • Returns any type Z
  • returns: an array with elements of type Z

.map( calls the function once for each element in the array. Each call passes the current element in as a parameter to the function.

Your function 'maps' the element in some way to something new.

It returns an array the same length as the original array, but with new values based on the 'map' you did in your function.

lodash: _.map(array, function)

Examples

[5, 9, 6].map(i => i + 2)
// => [7, 11, 8]
const arrayOfObjects = [{a: 1, b: 'green'}, {a: 5, b: 'red'}, {a: 3, b: 'blue'}]

arrayOfObjects.map(object => object.b)
// => ['green', 'red', 'blue']


arrayOfObjects.map((object, index) => {
  const asString = `index: ${index}, a: ${object.a}, b: ${object.b}`
  return asString
})
// => [ "index: 0, a: 1, b: green", "index: 1, a: 5, b: red", "index: 2, a: 3, b: blue" ]

array.find(function)

  • array is an array with elements of type T
  • function is a function:
    • Parameter 1: Element of type T
    • Parameter 2 (Optional): Index of array (0, 1, 2...) of type number
    • Returns a boolean
  • returns: a the first element (type T) of the array where the function returns true

array.find(function) calls function for each element in the array until the function returns true. Once the function returns true it stops and returns that array element.

lodash: _.find(array, function)

Examples

const arrayOfObjects = [{a: 1, b: 'green'}, {a: 5, b: 'red'}, {a: 3, b: 'blue'}]

arrayOfObjects.find(object => object.a === 5)
// => {a: 5, b: 'red'}


arrayOfObjects.find(object => object.b === 'green')
// => {a: 1, b: 'green'}


arrayOfObjects.find((object, index) => {
  console.log(index)
  return object.b === 'green'
})
// => {a: 1, b: 'green'}
// prints: 0 - find stops after it finds the object it was looking for.

array.findIndex(function)

Same as array.find(function) but instead of returning the first element where function returns true, it returns the index of the element.

lodash: _.findIndex(array, function)

Examples

const arrayOfObjects = [{a: 1, b: 'green'}, {a: 5, b: 'red'}, {a: 3, b: 'blue'}]

arrayOfObjects.findIndex(object => object.a === 5)
// => 1
// arrayOfObjects[1] is {a: 5, b: 'red'}

array.some(function)

  • array is an array with elements of type T
  • function is a function:
    • Parameter 1: Element of type T
    • Parameter 2 (Optional): Index of array (0, 1, 2...) of type number
    • Returns a boolean
  • returns: a boolean

.some( calls the function once for each element in the array. Each call passes the current element in as a parameter to the function. If the function returns true for any of the elements .some( trues true.

In English: "Do 'some' of the function calls return true"

lodash: _.some(array, function)

Examples:

[1, 2, 3].some(x => x > 4)
// => false
[1, 2, 3].some(x => x < 4)
// => true
[1, 2, 3].some(x => x === 3)
// => true
// only the last element returns true, but that's enough and the whole thing returns true.

array.every(function)

  • array is an array with elements of type T
  • function is a function:
    • Parameter 1: Element of type T
    • Parameter 2 (Optional): Index of array (0, 1, 2...) of type number
    • Returns a boolean
  • returns: a boolean

.every( calls the function once for each element in the array. Each call passes the current element in as a parameter to the function. If the function returns true for all of the elements .every( trues true.

In English: "Do 'every' one of the function calls return true"

lodash: _.every(array, function)

Examples:

[1, 2, 3].every(x => x > 4)
// => false
// None of them return true!
[1, 2, 3].every(x => x < 4)
// => true
[1, 2, 3].every(x => x === 3)
// => false
// only the last element returns true, that's not 'every' one, so every returns false

_.sortBy(array, function)

  • array is an array with elements of type T
  • function is a function:
    • Parameter 1: Element of type T
    • Parameter 2 (Optional): Index of array (0, 1, 2...) of type number
    • Returns any type Z
  • returns: an array with elements of type T sorted by Z

_.sortBy 'maps' over the elements in the array calling the function for each element. Those results are then used to sort the original items of the array.

Examples

const people = [{name: 'Jess', age: 31}, {name: 'Tom', age: 29}, {name: 'Rosa', age: 29}, {name: 'Louie', age: 31}]

_.sortBy(people, person => person.name)
// => [{name: 'Jess', age: 31}, {name: 'Louie', age: 31}, {name: 'Rosa', age: 29}, {name: 'Tom', age: 29}]
// alphabetical order by name!

_.sortBy(people, person => person.name).reverse()
// => [{name: 'Tom', age: 29}, {name: 'Rosa', age: 29}, {name: 'Louie', age: 31}, {name: 'Jess', age: 31}]
// reverse alphabetical order by name (by chaining .reverse() on the end)

_.sortBy(people, person => person.age)
// => [{name: 'Tom', age: 29}, {name: 'Rosa', age: 29}, {name: 'Jess', age: 31}, {name: 'Louie', age: 31}]

_.groupBy(array, function)

  • array is an array with elements of type T
  • function is a function:
    • Parameter 1: Element of type T
    • Parameter 2 (Optional): Index of array (0, 1, 2...) of type number
    • Returns any type Z
  • returns: an object where the keys are of type Z with values which are arrays of type T

_.groupBy 'maps' over the elements in the array calling the function for each element. Those results are then used to group the original items of the array into multiple arrays. The returned object's keys are the 'map'd values and the values are the original array elements which 'map'd to that value.

_.groupBy(['apple', 'pear', 'melon'], s => s.length)

// => {4: ['pear'], 5: ['apple', 'melon']}
const people = [{name: 'Jess', age: 31}, {name: 'Tom', age: 29}, {name: 'Rosa', age: 29}, {name: 'Louie', age: 31}]

_.groupBy(people, person => person.age)
// => {31: [{name: 'Jess', age: 31}, {name: 'Louie', age: 31}], 29: [{name: 'Tom', age: 29}, {name: 'Rosa', age: 29}]}