How to use the rest function from underscore

Find comprehensive JavaScript underscore.rest code examples handpicked from public code repositorys.

underscore.rest is a function in the Underscore.js library that returns a new function that passes a variable number of arguments to a given function after a specified number of arguments.

5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
// Get the last element of an array. Passing **n** will return the last N
// values in the array.
_.last = function(array, n, guard) {
  if (array == null) return void 0;
  if (n == null || guard) return array[array.length - 1];
  return _.rest(array, Math.max(0, array.length - n));
};

// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
// Especially useful on the arguments object. Passing an **n** will return
fork icon0
star icon2
watch icon0

How does underscore.rest work?

underscore.rest is a function provided by the Underscore.js library that returns a new function that passes a variable number of arguments to a given function after a specified number of arguments. To use underscore.rest, you can call the rest function and pass in the original function you want to modify and the number of arguments to omit. The rest function will then return a new function that takes a variable number of arguments, but only passes the arguments after the specified number of arguments to the original function. For example, if you pass in an original function with three arguments and a value of 2 for the number of arguments to omit, the new function will take any number of arguments but only pass the third argument and all subsequent arguments to the original function. The resulting new function can be used to partially apply arguments to a function, or to create a new function that accepts a variable number of arguments. This functionality can be useful when working with functions that take a variable number of arguments, or when you need to create a new function that omits the first few arguments of an existing function. underscore.rest is one of many functions provided by the Underscore.js library that simplify working with arrays, objects, and functions in JavaScript.

Ai Example

1
2
3
4
5
6
7
8
9
const _ = require("underscore");

function myFunction(a, b, c, d) {
  console.log(a, b, c, d);
}

const myNewFunction = _.rest(myFunction, 2);

myNewFunction(1, 2, 3, 4, 5); // Logs: 3 4 5

In this example, we define a function myFunction that takes four arguments and logs them to the console. We then use _.rest to create a new function myNewFunction that omits the first two arguments of myFunction. We pass in myFunction as the original function and 2 as the number of arguments to omit. Finally, we call myNewFunction with five arguments, which are all passed to myFunction after the first two arguments are omitted. The resulting output logs 3 4 5 to the console, which corresponds to the values of the third, fourth, and fifth arguments passed to myNewFunction. This example demonstrates how you can use underscore.rest to create a new function that omits the first few arguments of an existing function.