Core Features

Arrays

  • _.chunk(array, [size=1]): Splits an array into chunks of a specified size. For example, _.chunk([1, 2, 3, 4], 2) results in [[1, 2], [3, 4]].
  • _.compact(array): Creates a new array with all falsy values removed. For instance, _.compact([0, 1, false, 2, '', 3]) results in [1, 2, 3].
  • _.concat(array, [values]): Merges multiple arrays into one. For example, _.concat([1], 2, [3], [[4]]) results in [1, 2, 3, [4]].
  • _.difference(array, [values]): Creates an array of values that are not included in the other given arrays. For example, _.difference([2, 1], [2, 3]) results in [1].

Objects

  • _.assign(object, [sources]): Assigns properties from source objects to a target object. For example, _.assign({ 'a': 1 }, { 'b': 2 }, { 'c': 3 }) results in { 'a': 1, 'b': 2, 'c': 3 }.
  • _.get(object, path, [defaultValue]): Retrieves the value at a given path of an object. If the resolved value is undefined, the defaultValue is returned. For example, _.get({ 'a': { 'b': 2 } }, 'a.b') results in 2.
  • _.has(object, path): Checks if a path exists in an object. For example, _.has({ 'a': { 'b': 2 } }, 'a.b') returns true.
  • _.merge(object, [sources]): Recursively merges properties of source objects into a target object. For example, _.merge({ 'a': { 'b': 2 } }, { 'a': { 'c': 3 } }) results in { 'a': { 'b': 2, 'c': 3 } }.

Strings

  • _.camelCase(string): Converts a string to camel case. For instance, _.camelCase('Foo Bar') results in fooBar.
  • _.capitalize(string): Capitalizes the first letter of a string. For example, _.capitalize('hello') results in Hello.
  • _.escape(string): Escapes special characters in a string to HTML entities. For instance, _.escape('<div>') results in &lt;div&gt;.
  • _.kebabCase(string): Converts a string to kebab case. For example, _.kebabCase('Foo Bar') results in foo-bar.

Functions

  • _.debounce(func, [wait=0], [options]): Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time it was invoked. This is useful for optimizing performance in scenarios such as handling input events.
  • _.throttle(func, [wait=0], [options]): Creates a throttled function that only invokes func at most once per every wait milliseconds. This can help ensure that functions are not called too frequently, such as during scroll or resize events.
  • _.once(func): Creates a function that is only invoked once. Useful for initializing something only once, such as setting up an event listener.

Collections

  • _.each(collection, iteratee): Iterates over elements of a collection (array or object) and executes a function for each element. For example, _.each([1, 2, 3], function(value) { console.log(value); }) logs each value to the console.
  • _.filter(collection, predicate): Creates an array of elements that pass a predicate function. For instance, _.filter([1, 2, 3, 4], function(n) { return n % 2 === 0; }) results in [2, 4].
  • _.map(collection, iteratee): Creates an array of values by running each element in a collection through a function. For example, _.map([1, 2, 3], function(n) { return n * 2; }) results in [2, 4, 6].

Utilities

  • _.identity(value): Returns the input value. This is useful as a default function for situations where you want to pass a function that does nothing to another function.
  • _.noop(): A no-operation function that returns undefined. This is useful as a placeholder or default function.
  • _.random([lower=0], [upper=1], [floating]): Generates a random number between lower and upper. For example, _.random(0, 5) might result in 3.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *