How to use the unary function from lodash

Find comprehensive JavaScript lodash.unary code examples handpicked from public code repositorys.

lodash.unary is a function in the Lodash library that creates a new function that accepts only one argument, ignoring any additional arguments passed to it.

406
407
408
409
410
411
412
413
414
415
module.exports.trimEnd             = _.trimEnd;
module.exports.trimStart           = _.trimStart;
module.exports.truncate            = _.truncate;
module.exports.truthy              = _.truthy;
module.exports.truthyAll           = _.truthyAll;
module.exports.unary               = _.unary;
module.exports.unescape            = _.unescape;
module.exports.union               = _.union;
module.exports.unionBy             = _.unionBy;
module.exports.unionWith           = _.unionWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

40
41
42
43
44
45
46
47
48
49
50
const benchmarks = [
  {
    name: 'unary',
    benchmarks: () => {
      const iirisUnary = I.unary((a, b) => b)
      const lodashUnary = _.unary((a, b) => b)
      const ramdaUnary = R.unary((a, b) => b)
      const nativeUnary = ((fn) => (x) => fn(x))((a, b) => b)


      return {
fork icon1
star icon31
watch icon0

How does lodash.unary work?

lodash.unary is a function provided by the Lodash library that creates a new function that accepts only one argument, ignoring any additional arguments that may be passed to it. It takes an existing function as input and returns a new function that wraps the original function and applies it to only one argument.

353
354
355
356
357
358
359
360
361
362
363
364
365
console.log(spread([1, 2, 3])); // => 6


const throttle = _.throttle(() => console.log('hello'), 100);
throttle();


const unary = _.unary(n => n + 1);
console.log(unary(1, 2)); // => 2


const wrap = _.wrap(n => n + 1, n => n * 2);
console.log(wrap(1)); // => 4
fork icon0
star icon4
watch icon0

+ 15 other calls in file

Ai Example

1
2
const array = ["1", "2", "3"];
const parsedArray = array.map(lodash.unary(parseInt)); // ['1', '2', '3']

In the example above, lodash.unary is used to create a function that takes only one argument. This is used to pass the parseInt function to the map method on an array of strings, so that each string can be parsed as an integer. The resulting parsedArray contains the integers 1, 2, and 3.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)