Skip to content

Instantly share code, notes, and snippets.

@tiggerk
Last active January 6, 2018 05:26
Show Gist options
  • Select an option

  • Save tiggerk/6130b0665bafa84a451429435a027416 to your computer and use it in GitHub Desktop.

Select an option

Save tiggerk/6130b0665bafa84a451429435a027416 to your computer and use it in GitHub Desktop.
You Might Not Need Underscore (ES6, ES2015)

Arrays

Iterate

  • Underscore

    _.each(array, iteratee)
    
    
  • ES5.1

    array.forEach(iteratee)
    
    

Map

  • Underscore

    _.map(array, iteratee)
    
    
  • ES5.1

    array.map(iteratee)
    
    

Use a function to accumulate a single value from an array (left-to-right)

  • Underscore

    _.reduce(array, iteratee, memo)
    
    
  • ES5.1

    array.reduce(iteratee, memo)
    
    

Use a function to accumulate a single value from an array (right-to-left)

  • Underscore

    _.reduceRight(array, iteratee, memo)
    
    
  • ES5.1

    array.reduceRight(iteratee, memo)
    
    

Test whether all elements in an array pass a predicate

  • Underscore

    _.every(array, predicate)
    
    
  • ES5.1

    array.every(predicate)
    
    

Test whether some element in an array passes a predicate

  • Underscore

    _.some(array, predicate)
    
    
  • ES5.1

    array.some(predicate)
    
    

Find a value in an array

  • Underscore

    _.find(array, predicate)
    
    
  • ES2015

    array.find(predicate)
    
    

Get a property from each element in an array

  • Underscore

    _.pluck(array, propertyName)
    
    
  • ES2015

    array.map(value => value[propertyName])
    
    

Check if array includes an element

  • Underscore

    _.includes(array, element)
    
    
  • ES2016

    array.includes(element)
    
    

Convert an array-like object to array

  • Underscore

    _.toArray(arguments)
    
    
  • ES2015

    [...arguments]
    
    

Convert an array of keys and values to an object

  • Underscore

    _.object(array)
    
    
  • ES2015

    array.reduce((result, [key, val]) => Object.assign(result, {[key]: val}), {})
    
    
  • Object Rest/Spread (Stage 2)

    array.reduce((result, [key, val]) => {...result, [key]: val}, {})
    
    

Create a copy of an array with all falsy values removed

  • Underscore

    _.compact(array)
    
    
  • ES5.1

    array.filter(Boolean)
    
    
  • ES2015

    array.filter(x => !!x)
    
    

Create a copy of an array with duplicates removed

  • Underscore

    _.uniq(array)
    
    
  • ES2015

    [...new Set(array)]
    
    

Find the index of a value in an array

  • Underscore

    _.indexOf(array, value)
    
    
  • ES5.1

    array.indexOf(value)
    
    

Find the index in an array by predicate

  • Underscore

    _.findIndex([4, 6, 7, 12], isPrime);
    
    
  • ES2015

    [4, 6, 7, 12].findIndex(isPrime);
    
    

Create an array with n numbers, starting from x

  • Underscore

    _.range(x, x + n)
    
    
  • ES2015

    Array.from(Array(n), (_, i) => x + i)
    
    

Objects

Names of own enumerable properties as an array

  • Underscore

    _.keys(object)
    
    
  • ES5.1

    Object.keys(object)
    
    

Number of keys in an object

  • Underscore

    _.size(object)
    
    
  • ES5.1

    Object.keys(object).length
    
    

Names of all enumerable properties as an array

  • Underscore

    _.allKeys(object)
    
    
  • ES2015

    [...Reflect.enumerate(object)]
    
    

Values

  • Underscore

    _.values(object)
    
    
  • ES2015

    Object.keys(object).map(key => object[key])
    
    

Create a new object with the given prototype and properties

  • Underscore

    _.create(proto, properties)
    
    
  • ES2015

    Object.assign(Object.create(proto), properties)
    
    

Create a new object from merged own properties

  • Underscore

    _.assign({}, source, { a: false })
    
    
  • ES2015

    Object.assign({}, source, { a: false })
    
    
  • Object Rest/Spread (Stage 2)

    { ...source, a: false }
    
    

Create a shallow clone of own properties of an object

  • Underscore

    _.extendOwn({}, object)
    
    
  • Object Rest/Spread (Stage 2)

    { ...object }
    
    

Check if an object is an array

  • Underscore

    _.isArray(object)
    
  • ES5.1

    Array.isArray(object)
    
    

Check if an object is a finite Number

  • Underscore

    _.isNumber(object) && _.isFinite(object)
    
    
  • ES2015

    Number.isFinite(object)
    
    

Functions

Bind a function to an object

  • Underscore

    foo(_.bind(function () {
      this.bar();
    }, this));
    
    foo(_.bind(object.fun, object));
    
    
  • ES2015

    foo(() => {
      this.bar();
    });
    
    foo(() => object.fun());
    
    

Utility

Identity function

  • Underscore

    _.identity
    
    
  • ES2015

    value => value
    
    

A function that returns a value

  • Underscore

    _.constant(value)
    
    
  • ES2015

    () => value
    
    

The empty function

  • Underscore

    _.noop
    
    
  • ES2015

    () => {}
    
    

Get the current time in milliseconds since the epoch

  • Underscore

    _.now()
    
    
  • ES5.1

    Date.now()
    
    

Template

  • Underscore

    var greeting = _.template("hello <%= name %>");
    greeting({ name: 'moe' });
    
    
  • ES2015

    const greeting = ({ name }) => `hello ${name}`;
    greeting({ name: 'moe' });
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment