add after ary assign assignIn assignInWith assignWith at before bind bindAll bindKey camelCase capitalize castArray ceil chain chunk clamp clone cloneDeep cloneDeepWith cloneWith compact concat conformsTo countBy create curry curryRight debounce deburr defaults defaultsDeep defer delay difference divide drop dropRight each eachRight endsWith entries entriesIn eq escape every extend extendWith fill filter find findIndex findKey findLast findLastIndex findLastKey first flatMap flatMapDeep flatMapDepth flatten flattenDeep flattenDepth flip floor forEach forEachRight forIn forInRight forOwn forOwnRight fromPairs functions functionsIn get groupBy gt gte has hasIn head inRange includes indexOf initial intersection invert invertBy invoke invokeMap isArguments isArray isArrayBuffer isArrayLike isArrayLikeObject isBoolean isBuffer isDate isElement isEmpty isEqual isEqualWith isError isFinite isFunction isInteger isLength isMap isMatch isMatchWith isNaN isNative isNil isNull isNumber isObject isObjectLike isPlainObject isRegExp isSafeInteger isSet isString isSymbol isTypedArray isUndefined isWeakMap isWeakSet join kebabCase keyBy keys keysIn last lastIndexOf lowerCase lowerFirst lt lte map mapKeys mapValues max maxBy mean meanBy memoize merge mergeWith min minBy multiply negate nth omit omitBy once orderBy overArgs pad padEnd padStart parseInt partial partialRight partition pick pickBy pull pullAll pullAllBy pullAllWith pullAt random rearg reduce reduceRight reject remove repeat replace rest result reverse round sample sampleSize set setWith shuffle size slice snakeCase some sortBy sortedIndex sortedIndexOf sortedLastIndex sortedLastIndexOf sortedUniq split spread startCase startsWith subtract sum sumBy tail take takeRight tap template throttle thru toArray toFinite toInteger toLength toLower toNumber toPairs toPairsIn toPlainObject toSafeInteger toString toUpper transform trim trimEnd trimStart truncate unary unescape union uniq uniqBy uniqWith unset unzip unzipWith update updateWith upperCase upperFirst values valuesIn without words wrap wrapperAt wrapperChain wrapperCommit wrapperFlatMap wrapperLodash wrapperNext wrapperPlant wrapperReverse wrapperToIterator wrapperValue xor zip zipObject zipObjectDeep zipWith

Lodash _.partialRight


Lodash is a popular JavaScript utility library that provides versatile functions to manipulate and work with arrays, collections, and objects. One of the many helpful methods in Lodash is _.partialRight. This method allows you to create a new function with preset arguments in the right-most position of the original function.

To understand _.partialRight better, let's first consider how arguments are passed in JavaScript functions. When you call a function with arguments, each argument is mapped to a corresponding parameter in the function declaration. For example:

function add(a, b) {
  return a + b;
add(2, 3); // returns 5

In this example, when we call add(2, 3), the value 2 is mapped to the parameter a, and the value 3 is mapped to the parameter b.

But what if we wanted to create a new function that calls add with b already set to 3? This is where _.partialRight comes in.

const add3 = _.partialRight(add, 3);
add3(2); // returns 5

In this example, _.partialRight creates a new function add3 that calls the original add function with 3 as the last argument. We then call add3(2), which is equivalent to calling add(2, 3), and returns 5.

_.partialRight also allows you to provide additional arguments that will come before the preset right-most arguments. For example:

function multiply(a, b, c) {
  return a * b * c;
const multiplyBy3 = _.partialRight(multiply, 3);
multiplyBy3(4, 5); // returns 60

In this example, multiplyBy3 is a new function that calls multiply with its second argument already set to 3. However, when we call multiplyBy3(4, 5), the value 4 is still mapped to the first parameter a, and the value 5 is mapped to the parameter b. The 3 argument is used as the last argument, multiplying a, b, and 3 together to return 60.

_.partialRight is particularly helpful for creating new functions based on existing functions that have a lot of arguments or complex parameter orders. By providing preset arguments to _.partialRight you can simplify the function signature, making it more concise and easier to use in other parts of your codebase.

In conclusion, _.partialRight is a useful method in Lodash that allows you to create a new function with preset arguments in the right-most position of the original function. Whether you need to simplify your function signature or fixate a specific argument in a function call, _.partialRight can help.