YOU MIGHT NOT NEED LODASH

Lodash is a great library, well crafted, battle tested and with a strong team.
Just to be clear: You should use Lodash - just make sure you import the bits you need, not the whole library to use one method.

This page is a mere exercise to see how can achieve the same functionalities in plain js: sometimes it's nice to understand the nitty gritty details of what we are building or how stuff works behind the hood.

The project is far from complete: this is the list of Lodash methods left. Please contribute on github.

array

chunk

lodash

// https://lodash.com/docs/#chunk
import { chunk } from 'lodash'

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 3)
// => [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 0)
// => []

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], -1)
// => []

plain js

const chunk = (arr, chunkSize = 1, cache = []) => {
  const tmp = [...arr]
  if (chunkSize <= 0) return cache
  while (tmp.length) cache.push(tmp.splice(0, chunkSize))
  return cache
}

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 3)
// => [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 0)
// => []

chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], -1)
// => []

compact

lodash

// https://lodash.com/docs/#compact
import { compact } from 'lodash'

compact([0, 1, 2])
// => [1, 2]

plain js

[0, 1, 2].filter(x => !!x)
// => [1, 2]

concat

lodash

// https://lodash.com/docs/#concat
import { concat } from 'lodash'

concat([1], 2, [3], [[4]])
// => [1, 2, 3, [4]]

plain js

[1].concat(2, [3], [[4]])
// => [1, 2, 3, [4]]

difference

lodash

// https://lodash.com/docs/#difference
import { difference } from 'lodash'

difference([2, 1], [3, 2])
// => [1]

plain js

[2, 1].filter(x => ![3, 2].includes(x))
// => [1]

differenceBy

lodash

// https://lodash.com/docs/#differenceBy
import { differenceBy } from 'lodash'

differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor)
// => [1.2]

// The `_.property` iteratee shorthand.
differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], 'x')
// => [{ 'x': 2 }]

plain js

[2.1, 1.2].filter(a => ![2.3, 3.4].map(Math.floor).includes(Math.floor(a)))
// => [1.2]

// The `_.property` iteratee shorthand.
[{ x: 2 }, { x: 1 }].filter(a => ![{ x: 1 }].map(b => b.x).includes(a.x))
// => [{ 'x': 2 }]

drop

lodash

// https://lodash.com/docs/#drop
import { drop } from 'lodash'

drop([1, 2, 3])
// => [2, 3]

drop([1, 2, 3], 2)
// => [3]

drop([1, 2, 3], 5)
// => []

drop([1, 2, 3], 0)
// => [1, 2, 3]

plain js

const drop = (arr, n = 1) => arr.slice(n)

drop([1, 2, 3])
// => [2, 3]

drop([1, 2, 3], 2)
// => [3]

drop([1, 2, 3], 5)
// => []

drop([1, 2, 3], 0)
// => [1, 2, 3]

dropRight

lodash

// https://lodash.com/docs/#dropRight
import { dropRight } from 'lodash'

dropRight([1, 2, 3])
// => [1, 2]

dropRight([1, 2, 3], 2)
// => [1]

dropRight([1, 2, 3], 5)
// => []

dropRight([1, 2, 3], 0)
// => [1, 2, 3]

plain js

const dropRight = (arr, n = 1) => (n && arr.slice(0, -n)) || arr

dropRight([1, 2, 3])
// => [1, 2]

dropRight([1, 2, 3], 2)
// => [1]

dropRight([1, 2, 3], 5)
// => []

dropRight([1, 2, 3], 0)
// => [1, 2, 3]

fill

lodash

// https://lodash.com/docs/#fill
import { fill } from 'lodash'

fill([1, 2, 3], 'a')
// => ['a', 'a', 'a']

fill(Array(3), 2)
// => [2, 2, 2]

fill([4, 6, 8, 10], '*', 1, 3)
// => [4, '*', '*', 10]

plain js

[1, 2, 3].fill('a')
// => ['a', 'a', 'a']

Array(3).fill(2)
// => [2, 2, 2]

[4, 6, 8, 10].fill('*', 1, 3)
// => [4, '*', '*', 10]

findIndex

lodash

// https://lodash.com/docs/#findIndex
import { findIndex } from 'lodash'

const users = [
  { user: 'barney', age: 36, active: true },
  { user: 'fred', age: 40, active: false },
  { user: 'pebbles', age: 1, active: true },
]

findIndex(users, o => o.age >= 40)
// => 1

plain js

const users = [
  { user: 'barney', age: 36, active: true },
  { user: 'fred', age: 40, active: false },
  { user: 'pebbles', age: 1, active: true },
]

users.findIndex(o => o.age >= 40)
// => 1

flatten

lodash

// https://lodash.com/docs/#flatten
import { flatten } from 'lodash'

flatten([1, [2, [3, [4]], 5]])
// => [1, 2, [3, [4]], 5]

plain js

[1, [2, [3, [4]], 5]].reduce((a, b) => a.concat(b), [])
// => [1, 2, [3, [4]], 5]

flattenDeep

lodash

// https://lodash.com/docs/#flattenDeep
import { flattenDeep } from 'lodash'

flattenDeep([1, [2, [3, [4]], 5]])
// => [1, 2, 3, 4, 5]

plain js

const flattenDeep = arr =>
  Array.isArray(arr) ? arr.reduce((a, b) => [...flattenDeep(a), ...flattenDeep(b)], []) : [arr]

flattenDeep([1, [[2], [3, [4]], 5]])
// => [1, 2, 3, 4, 5]

fromPairs

lodash

// https://lodash.com/docs/#fromPairs
import { fromPairs } from 'lodash'

fromPairs([['a', 1], ['b', 2]])
// => { 'a': 1, 'b': 2 }

plain js

const fromPairs = pairs =>
  pairs.reduce((cache, pair) => {
    cache[pair[0]] = pair[1]
    return cache
  }, {})

fromPairs([['a', 1], ['b', 2]])
// => { 'a': 1, 'b': 2 }

head

lodash

// https://lodash.com/docs/#head
import { head } from 'lodash'

head([1, 2, 3])
// => 1

plain js

const [head, ...tail] = [1, 2, 3] // eslint-disable-line
head
// => 1

indexOf

lodash

// https://lodash.com/docs/#indexOf
import { indexOf } from 'lodash'

indexOf([1, 2, 3], 3)
// => 2

plain js

[1, 2, 3].indexOf(3)
// => 2

initial

lodash

// https://lodash.com/docs/#initial
import { initial } from 'lodash'

initial([1, 2, 3])
// => [1, 2]

plain js

[1, 2, 3].slice(0, -1)
// => [1, 2]

join

lodash

// https://lodash.com/docs/#join
import { join } from 'lodash'

join(['a', 'b', 'c'], '~')
// => 'a~b~c'

plain js

['a', 'b', 'c'].join('~')
// => 'a~b~c'

last

lodash

// https://lodash.com/docs/#last
import { last } from 'lodash'

last(['a', 'b', 'c'])
// => 'c'

plain js

const arr = ['a', 'b', 'c']

arr[arr.length - 1]
// => 'c'

lastIndexOf

lodash

// https://lodash.com/docs/#lastIndexOf
import { lastIndexOf } from 'lodash'

lastIndexOf([1, 2, 1, 2], 2)
// => 3

// Search from the `fromIndex`
lastIndexOf([1, 2, 1, 2], 2, 2)
// => 1

plain js

[1, 2, 1, 2].lastIndexOf(2)
// => 3

// Search from the `fromIndex`
[1, 2, 1, 2].lastIndexOf(2, 2)
// => 1

reject

lodash

// https://lodash.com/docs/#reject
import { reject } from 'lodash'

reject(['a', 'b', 'c', 'd', 'e', 'f', 'g'], char => char === 'd')
// => [['a', 'b', 'c', 'e', 'f', 'g']]

plain js

['a', 'b', 'c', 'd', 'e', 'f', 'g'].filter(char => char !== 'd')
// => [['a', 'b', 'c', 'e', 'f', 'g']]

tail

lodash

// https://lodash.com/docs/#tail
import { tail } from 'lodash'

tail([1, 2, 3])
// => [2, 3]

plain js

const [head, ...tail] = [1, 2, 3] //eslint-disable-line
tail
// => [2, 3]

uniq

lodash

// https://lodash.com/docs/#uniq
import { uniq } from 'lodash'

uniq([2, 1, 2])
// => [2, 1]

plain js

[...new Set([2, 1, 2])]
// => [2, 1]

collection

every

lodash

// https://lodash.com/docs/#every
import { every } from 'lodash'

function isLargerThanTen(element) {
  return element >= 10
}

every([10, 20, 30], isLargerThanTen)
// => true

plain js

function isLargerThanTen(element) {
  return element >= 10
}

[10, 20, 30].every(isLargerThanTen)
// => true

filter

lodash

// https://lodash.com/docs/#filter
import { filter } from 'lodash'

function isBigEnough(value) {
  return value >= 10
}

filter([12, 5, 8, 130, 44], isBigEnough)
// => [12, 130, 44]

plain js

function isBigEnough(value) {
  return value >= 10
}

[12, 5, 8, 130, 44].filter(isBigEnough)
// => [12, 130, 44]

find

lodash

// https://lodash.com/docs/#find
import { find } from 'lodash'

const users = [
  { user: 'barney', age: 36, active: true },
  { user: 'fred', age: 40, active: false },
  { user: 'pebbles', age: 1, active: true },
]

find(users, o => o.age < 40)
// => { user: "barney", age: 36, active: true }

plain js

const users = [
  { user: 'barney', age: 36, active: true },
  { user: 'fred', age: 40, active: false },
  { user: 'pebbles', age: 1, active: true },
]

users.find(o => o.age < 40)
// => { user: "barney", age: 36, active: true }

forEach

lodash

// https://lodash.com/docs/#forEach
import { forEach } from 'lodash'

function iteratee(item, index) {
  console.log(item, index)
}

const array = ['a', 'b', 'c']

forEach(array, iteratee)
// => a 0
// => b 1
// => c 2

plain js

function iteratee(item, index) {
  console.log(item, index)
}

;['a', 'b', 'c'].forEach(iteratee)
// => a 0
// => b 1
// => c 2

forEachRight

lodash

// https://lodash.com/docs/#forEachRight
import { forEachRight } from 'lodash'

function iteratee(item, index) {
  console.log(item, index)
}

const array = ['a', 'b', 'c']

forEachRight(array, iteratee)
// => c 2
// => b 1
// => a 0

plain js

function forEachRight(array, iteratee) {
  array
    .slice()
    .reverse()
    .forEach((val, index) => {
      iteratee(val, Math.abs(array.length - index), array)
    })
}

function iteratee(item, index) {
  console.log(item, index)
}

const array = ['a', 'b', 'c']

forEachRight(array, iteratee)
// => c 2
// => b 1
// => a 0

get

lodash

// https://lodash.com/docs/#get
import { get } from 'lodash'

const simpleObject = { a: { b: 2 } }
const complexObject = { a: [{ b: { c: 3 } }] }

get(simpleObject, 'a.b')
// => 2
get(complexObject, 'a[0].b.c')
// => 3
get(complexObject, ['a', '0', 'b', 'c'])
// => 2
get(simpleObject, 'a.b.c', 'default')
// => 'default'
get(complexObject, 'a.b.c', 'default')
// =>  'default'
get(complexObject, null)
// =>  undefined

plain js

const get = (object, path, value) => {
  // If path is not defined or it has false value
  if (!path) return undefined
  // Check if path is string or array. Regex : ensure that we do not have '.' and brackets
  const pathArray = Array.isArray(path) ? path : path.split(/[,[\].]/g).filter(Boolean)
  // Find value if exist return otherwise return undefined value;
  return pathArray.reduce((prevObj, key) => prevObj && prevObj[key], object) || value
}

const simpleObject = { a: { b: 2 } }
const complexObject = { a: [{ b: { c: 3 } }] }

get(simpleObject, 'a.b')
// => 2
get(complexObject, 'a[0].b.c')
// => 3
get(complexObject, ['a', '0', 'b', 'c'])
// => 2
get(simpleObject, 'a.b.c', 'default')
// => 'default'
get(complexObject, 'a.b.c', 'default')
// =>  'default'
get(complexObject, null)
// =>  undefined

includes

lodash

// https://lodash.com/docs/#includes
import { includes } from 'lodash'

includes([1, 2, 3], 1)
// => true

plain js

;[1, 2, 3].includes(1)
// => true

[1, 2, 3].indexOf(1) > -1
// => true

map

lodash

// https://lodash.com/docs/#map
import { map } from 'lodash'

map([1, 2, 3], n => n * 3)
// => [3, 6, 9]

plain js

[1, 2, 3].map(n => n * 3)
// => [3, 6, 9]

reduce

lodash

// https://lodash.com/docs/#reduce
import { reduce } from 'lodash'

reduce([1, 2], (sum, n) => sum + n, 0)
// => 3

reduce(
  { a: 1, b: 2, c: 1 },
  (result, value, key) => {
    ;(result[value] || (result[value] = [])).push(key)
    return result
  },
  {}
)
// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)

plain js

[1, 2].reduce((sum, n) => sum + n, 0)
// => 3

Object.entries({ a: 1, b: 2, c: 1 }).reduce((result, [key, value]) => {
  ;(result[value] || (result[value] = [])).push(key)
  return result
}, {})
// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)

reduceRight

lodash

// https://lodash.com/docs/#reduceRight
import { reduceRight } from 'lodash'

reduceRight(
  [[0, 1], [2, 3], [4, 5]],
  (flattened, other) => flattened.concat(other),
  []
)
// => [4, 5, 2, 3, 0, 1]

plain js

[[0, 1], [2, 3], [4, 5]].reduceRight(
  (flattened, other) => flattened.concat(other),
  []
)
// => [4, 5, 2, 3, 0, 1]

size

lodash

// https://lodash.com/docs/#size
import { size } from 'lodash'

size([1, 2, 3])
// => 3

size({ a: 1, b: 2 })
// => 2

size('pebbles')
// => 7

plain js

const size = item => (item.constructor === Object ? Object.keys(item).length : item.length)

size([1, 2, 3])
// => 3

size({ a: 1, b: 2 })
// => 2

size('pebbles')
// => 7

some

lodash

// https://lodash.com/docs/#some
import { some } from 'lodash'

const isEven = n => n % 2 === 0

some([3, 4, 5], isEven)
// => true

plain js

const isEven = n => n % 2 === 0

[3, 4, 5].some(isEven)
// => true

date

now

lodash

// https://lodash.com/docs/#now
import { now } from 'lodash'

now()
// => # milliseconds  elapsed since the Unix epoch

plain js

Date.now()
// => # milliseconds  elapsed since the Unix epoch

function

ary

lodash

// https://lodash.com/docs/#ary
import { ary } from 'lodash'

['6', '8', '10'].map(ary(Number.parseInt, 1))
// => [6, 8, 10]

plain js

const ary = (fn, arity) => (...args) => fn(...args.slice(0, arity))

['6', '8', '10'].map(ary(Number.parseInt, 1))
// => [6, 8, 10]

unary

lodash

// https://lodash.com/docs/#unary
import { unary } from 'lodash'

['6', '8', '10'].map(unary(Number.parseInt))
// => [6, 8, 10]

plain js

const unary = fn => arg => fn(arg)

['6', '8', '10'].map(unary(Number.parseInt))
// => [6, 8, 10]

lang

gt

lodash

// https://lodash.com/docs/#gt
import { gt } from 'lodash'

gt(3, 1)
// => true

gt(3, 3)
// => false

gt(1, 3)
// => false

plain js

const gt = (a, b) => a > b

gt(3, 1)
// => true

gt(3, 3)
// => false

gt(1, 3)
// => false

gte

lodash

// https://lodash.com/docs/#gte
import { gte } from 'lodash'

gte(3, 1)
// => true

gte(3, 3)
// => true

gte(1, 3)
// => false

plain js

const gte = (a, b) => a >= b

gte(3, 1)
// => true

gte(3, 3)
// => true

gte(1, 3)
// => false

isArray

lodash

// https://lodash.com/docs/#isArray
import { isArray } from 'lodash'

isArray([1, 2, 3])
// => true

isArray(document.body.children)
// => false

isArray('abc')
// => false

plain js

Array.isArray([1, 2, 3])
// => true

Array.isArray(document.body.children)
// => false

Array.isArray('abc')
// => false

isFinite

lodash

// https://lodash.com/docs/#isFinite
import { isFinite } from 'lodash'

isFinite(3)
// => true

isFinite(Number.MIN_VALUE)
// => true

isFinite(Infinity)
// => false

isFinite('3')
// => false

plain js

Number.isFinite(3)
// => true

Number.isFinite(Number.MIN_VALUE)
// => true

Number.isFinite(Infinity)
// => false

Number.isFinite('3')
// => false

isInteger

lodash

// https://lodash.com/docs/#isInteger
import { isInteger } from 'lodash'

isInteger(3)
// => true

isInteger(Number.MIN_VALUE)
// => false

isInteger(Infinity)
// => false

isInteger('3')
// => false

plain js

Number.isInteger(3)
// => true

Number.isInteger(Number.MIN_VALUE)
// => false

Number.isInteger(Infinity)
// => false

Number.isInteger('3')
// => false

isNaN

lodash

// https://lodash.com/docs/#isNaN
import { isNaN } from 'lodash'

isNaN(NaN)
// => true

isNaN(Number(NaN))
// => true

isNaN(undefined)
// => false

plain js

Number.isNaN(NaN)
// => true

Number.isNaN(Number(NaN))
// => true

Number.isNaN(undefined)
// => false

isNil

lodash

// https://lodash.com/docs/#isNil
import { isNil } from 'lodash'

isNil(null)
// => true

isNil(undefined)
// => true

isNil(NaN)
// => false

plain js

const isNil = val => val == null

isNil(null)
// => true

isNil(undefined)
// => true

isNil(NaN)
// => false

isNull

lodash

// https://lodash.com/docs/#isNull
import { isNull } from 'lodash'

isNull(null)
// => true

isNull(undefined)
// => false

plain js

const isNull = val => val === null

isNull(null)
// => true

isNull(undefined)
// => false

isUndefined

lodash

// https://lodash.com/docs/#isUndefined
import { isUndefined } from 'lodash'

isUndefined(undefined)
// => true

isUndefined(null)
// => false

plain js

const isUndefined = val => val === undefined

isUndefined(undefined)
// => true

isUndefined(null)
// => false

math

add

lodash

// https://lodash.com/docs/#add
import { add } from 'lodash'

add(6, 4)
// => 10

plain js

const add = (a, b) => a + b

add(6, 4)
// => 10

ceil

lodash

// https://lodash.com/docs/#ceil
import { ceil } from 'lodash'

ceil(4.006)
// => 5

ceil(6.004, 2)
// => 6.01

ceil(6040, -2)
// => 6100

plain js

const ceil = (num, precision = 0) => {
  const modifier = 10 ** precision
  return !modifier ? Math.ceil(num) : Math.ceil(num * modifier) / modifier
}

ceil(4.006)
// => 5

ceil(6.004, 2)
// => 6.01

ceil(6040, -2)
// => 6100

divide

lodash

// https://lodash.com/docs/#divide
import { divide } from 'lodash'

divide(6, 4)
// => 1.5

plain js

const divide = (a, b) => a / b

divide(6, 4)
// => 1.5

floor

lodash

// https://lodash.com/docs/#floor
import { floor } from 'lodash'

floor(4.006)
// => 4

floor(0.046, 2)
// => 0.04

floor(4060, -2)
// => 4000

plain js

const floor = (num, precision = 0) => {
  const modifier = 10 ** precision
  return !modifier ? Math.floor(num) : Math.floor(num * modifier) / modifier
}

floor(4.006)
// => 4

floor(0.046, 2)
// => 0.04

floor(4060, -2)
// => 4000

max

lodash

// https://lodash.com/docs/#max
import { max } from 'lodash'

max([4, 2, 8, 6])
// => 8

max([])
// => undefined

plain js

const max = nums => (nums.length ? Math.max(...nums) : undefined)

max([4, 2, 8, 6])
// => 8

max([])
// => undefined

maxBy

lodash

// https://lodash.com/docs/#maxBy
import { maxBy } from 'lodash'

const objects = [{ n: 1 }, { n: 2 }]

maxBy(objects, o => o.n)
// => { 'n': 2 }

maxBy(objects, 'n')
// => { 'n': 2 }

plain js

const maxBy = (arr, iteratee) => {
  const func = typeof iteratee === 'function' ? iteratee : item => item[iteratee]
  const max = Math.max(...arr.map(func))
  return arr.find(item => func(item) === max)
}

const objects = [{ n: 1 }, { n: 2 }]

maxBy(objects, o => o.n)
// => { 'n': 2 }

maxBy(objects, 'n')
// => { 'n': 2 }

mean

lodash

// https://lodash.com/docs/#mean
import { mean } from 'lodash'

mean([4, 2, 8, 6])
// => 5

plain js

const mean = arr => arr.reduce((acc, num) => acc + num, 0) / arr.length

mean([4, 2, 8, 6])
// => 5

meanBy

lodash

// https://lodash.com/docs/#meanBy
import { meanBy } from 'lodash'

const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }]

meanBy(objects, o => o.n)
// => 5

meanBy(objects, 'n')
// => 5

plain js

const meanBy = (arr, iteratee) => {
  const func = typeof iteratee === 'function' ? iteratee : item => item[iteratee]
  return arr.reduce((acc, item) => acc + func(item), 0) / arr.length
}

const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }]

meanBy(objects, o => o.n)
// => 5

meanBy(objects, 'n')
// => 5

min

lodash

// https://lodash.com/docs/#min
import { min } from 'lodash'

min([4, 2, 8, 6])
// => 8

min([])
// => undefined

plain js

const min = nums => (nums.length ? Math.min(...nums) : undefined)

min([4, 2, 8, 6])
// => 8

min([])
// => undefined

minBy

lodash

// https://lodash.com/docs/#minBy
import { minBy } from 'lodash'

const objects = [{ n: 1 }, { n: 2 }]

minBy(objects, o => o.n)
// => { 'n': 1 }

minBy(objects, 'n')
// => { 'n': 1 }

plain js

const minBy = (arr, iteratee) => {
  const func = typeof iteratee === 'function' ? iteratee : item => item[iteratee]
  const min = Math.min(...arr.map(func))
  return arr.find(item => func(item) === min)
}

const objects = [{ n: 1 }, { n: 2 }]

minBy(objects, o => o.n)
// => { 'n': 1 }

minBy(objects, 'n')
// => { 'n': 1 }

multiply

lodash

// https://lodash.com/docs/#multiply
import { multiply } from 'lodash'

multiply(6, 4)
// => 24

plain js

const multiply = (a, b) => a * b

multiply(6, 4)
// => 24

round

lodash

// https://lodash.com/docs/#round
import { round } from 'lodash'

round(4.006)
// => 4

round(4.006, 2)
// => 4.01

round(4060, -2)
// => 4100

plain js

const round = (num, precision = 0) => {
  const modifier = 10 ** precision
  return !modifier ? Math.round(num) : Math.round(num * modifier) / modifier
}

round(4.006)
// => 4

round(4.006, 2)
// => 4.01

round(4060, -2)
// => 4100

subtract

lodash

// https://lodash.com/docs/#subtract
import { subtract } from 'lodash'

subtract(6, 4)
// => 2

plain js

const subtract = (a, b) => a - b

subtract(6, 4)
// => 2

sum

lodash

// https://lodash.com/docs/#sum
import { sum } from 'lodash'

sum([4, 2, 8, 6])
// => 20

plain js

const sum = arr =>
  arr.reduce((acc, num) => {
    acc += num
    return acc
  }, 0)

sum([4, 2, 8, 6])
// => 20

sumBy

lodash

// https://lodash.com/docs/#sumBy
import { sumBy } from 'lodash'

const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }]

sumBy(objects, o => o.n)
// => 20

sumBy(objects, 'n')
// => 20

plain js

const sumBy = (arr, iteratee) => {
  const func = typeof iteratee === 'function' ? iteratee : item => item[iteratee]

  return arr.reduce((acc, item) => acc + func(item), 0)
}

const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }]

sumBy(objects, o => o.n)
// => 20

// The `_.property` iteratee shorthand.
sumBy(objects, 'n')
// => 20

number

clamp

lodash

// https://lodash.com/docs/#clamp
import { clamp } from 'lodash'

clamp(-10, -5)
// => -10

clamp(-10, -5, 5)
// => -5

clamp(10, -5, 5)
// => 5

plain js

const clamp = (num, clamp, higher) =>
  higher ? Math.min(Math.max(num, clamp), higher) : Math.min(num, clamp)

clamp(-10, -5)
// => -10

clamp(-10, -5, 5)
// => -5

clamp(10, -5, 5)
// => 5

inRange

lodash

// https://lodash.com/docs/#inRange
import { inRange } from 'lodash'

inRange(3, 2, 4)
// => true

inRange(4, 8)
// => true

inRange(4, 2)
// => false

inRange(2, 2)
// => false

inRange(1.2, 2)
// => true

inRange(5.2, 4)
// => false

inRange(-3, -2, -6)
// => true

plain js

const inRange = (num, rangeStart, rangeEnd = 0) =>
  (rangeStart < num && num < rangeEnd) || (rangeEnd < num && num < rangeStart)

inRange(3, 2, 4)
// => true

inRange(4, 8)
// => true

inRange(4, 2)
// => false

inRange(2, 2)
// => false

inRange(1.2, 2)
// => true

inRange(5.2, 4)
// => false

inRange(-3, -2, -6)
// => true

object

pick

lodash

// https://lodash.com/docs/#pick
import { pick } from 'lodash'

const object = {
  a: 1,
  b: 2,
  c: 3,
}

pick(object, ['a', 'c'])
// => {a: 1, c: 3}

plain js

const object = {
  a: 1,
  b: 2,
  c: 3,
}

const { a, c } = object

{ a, c }
// => {a: 1, c: 3}

values

lodash

// https://lodash.com/docs/#values
import { values } from 'lodash'

const object = {
  0: {
    a: 1,
    b: 2,
    c: 3,
  },
  1: {
    d: 4,
    e: 5,
    f: 6,
  },
}

values(object)
// => [{a: 1, b: 2, c: 3}, {d: 4, e: 5, f: 6}]

plain js

const object = {
  0: {
    a: 1,
    b: 2,
    c: 3,
  },
  1: {
    d: 4,
    e: 5,
    f: 6,
  },
}

Object.values(object)
// => [{a: 1, b: 2, c: 3}, {d: 4, e: 5, f: 6}]

string

endsWith

lodash

// https://lodash.com/docs/#endsWith
import { endsWith } from 'lodash'

endsWith('pinecone', 'cone')
// => true

plain js

'pinecone'.endsWith('cone')
// => true

split

lodash

// https://lodash.com/docs/#split
import { split } from 'lodash'

split('a-b-c', '-')
// => ['a', 'b', 'c']

split('a-b-c', '-', 2)
// => ['a', 'b']

plain js

'a-b-c'.split('-')
// => ['a', 'b', 'c']

'a-b-c'.split('-').slice(0, 2)
// => ['a', 'b']

startsWith

lodash

// https://lodash.com/docs/#startsWith
import { startsWith } from 'lodash'

startsWith('JavaScript', 'Java')
// => true

plain js

'JavaScript'.startsWith('Java')
// => true

util

constant

lodash

// https://lodash.com/docs/#constant
import { constant } from 'lodash'

const return42 = constant(42)
return42()
// => 42

plain js

const constant = arg => () => arg
const return42 = constant(42)

return42()
// => 42

noop

lodash

// https://lodash.com/docs/#noop
import { noop } from 'lodash'

noop()
// => undefined

plain js

const noop = () => {}

noop()
// => undefined

stubArray

lodash

// https://lodash.com/docs/#stubArray
import { stubArray } from 'lodash'

stubArray()
// => []

plain js

const stubArray = () => []

stubArray()
// => []

stubFalse

lodash

// https://lodash.com/docs/#stubFalse
import { stubFalse } from 'lodash'

stubFalse()
// => false

plain js

const stubFalse = () => false

stubFalse()
// => false

stubObject

lodash

// https://lodash.com/docs/#stubObject
import { stubObject } from 'lodash'

stubObject()
// => {}

plain js

const stubObject = () => ({})

stubObject()
// => {}

stubString

lodash

// https://lodash.com/docs/#stubString
import { stubString } from 'lodash'

stubString()
// => ''

plain js

const stubString = () => ''

stubString()
// => ''

stubTrue

lodash

// https://lodash.com/docs/#stubTrue
import { stubTrue } from 'lodash'

stubTrue()
// => true

plain js

const stubTrue = () => true

stubTrue()
// => true

times

lodash

// https://lodash.com/docs/#times
import { times } from 'lodash'

times(3)
// => [0, 1, 2]

times(3, i => i + 3)
// => [3, 4, 5]

plain js

const times = (n, iteratee = i => i) => Array.from({ length: n }).map((_, i) => iteratee(i))

times(3)
// => [0, 1, 2]

times(3, i => i + 3)
// => [3, 4, 5]