method source npm

_.method(path, [args])

Creates a function that invokes the method at path of a given object. Any additional arguments are provided to the invoked method.

Arguments

  1. path (Array|string)

    The path of the method to invoke.

  2. [args] (...*)

    The arguments to invoke the method with.

Returns (Function)

Returns the new function.

Example

var objects = [
  { 'a': { 'b': { 'c': _.constant(2) } } },
  { 'a': { 'b': { 'c': _.constant(1) } } }
];

_.map(objects, _.method('a.b.c'));
// => [2, 1]

_.invoke(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
// => [1, 2]