How to use the modifyPath function from ramda

Find comprehensive JavaScript ramda.modifyPath code examples handpicked from public code repositorys.

ramda.modifyPath is a function in the Ramda library that applies a transformation function to a nested value within an object or array, based on a specified path.

193
194
195
196
197
198
199
200
201
202
            schema,
            apiPath,
            seededEntities,
        });

        resData = R.modifyPath([index], () => resEntry, resData);
    }

    return resData;
} else {
fork icon0
star icon3
watch icon1

+ 22 other calls in file

22
23
24
25
26
27
28
29
30
31
const pathName = key.split('.');
const valueEval = JSON.stringify(R.path(pathName, trimmedObj));
const keyEval = JSON.stringify(key);
if (sizeInKB(valueEval) > sizeInKB(keyEval)) {
  // remove the value
  trimmedObj = R.modifyPath(pathName, e => '...' , trimmedObj); 
} else {
  // remove the name
  trimmedObj = R.dissocPath(pathName, trimmedObj); 
}
fork icon0
star icon0
watch icon0

How does ramda.modifyPath work?

ramda.modifyPath allows you to modify a nested value in an object or array, based on a specified path. The function takes three arguments: a path as an array of keys or indices, a transformation function to apply to the target value, and an object or array to modify.

The function applies the transformation function to the target value at the end of the specified path, and returns a new object or array with the updated value. If any of the keys or indices in the path are not present in the object or array, ramda.modifyPath returns a new copy of the original data structure without any changes.

By default, ramda.modifyPath creates new objects or arrays as it traverses down the path, in order to avoid mutating the original data. You can disable this behavior by passing {clone: false} as an optional fourth argument.

If you need to modify a deeply nested value in an object or array, ramda.modifyPath can be a cleaner and more concise alternative to using multiple nested R.assocPath or R.update calls.

Note that ramda.modifyPath returns a new data structure, rather than modifying the original data in place. If you need to modify the original data directly, consider using a mutable data structure or a library like immer.

ramda.modifyPath is part of the Ramda library, which is a functional programming library for JavaScript.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const R = require("ramda");

const obj = {
  foo: {
    bar: {
      baz: 42,
    },
  },
};

const path = ["foo", "bar", "baz"];
const addOne = R.add(1);

const newObj = R.modifyPath(path, addOne, obj);

console.log(newObj); // { foo: { bar: { baz: 43 } } }

In this example, we have an object obj with a nested value 42. We define a path ['foo', 'bar', 'baz'] to specify the location of the value we want to modify. We also define a transformation function addOne, which will increment the target value by 1. We then call R.modifyPath with these arguments to modify the target value in obj. The function returns a new object newObj with the updated value. We log newObj to the console, which outputs { foo: { bar: { baz: 43 } } }.

Other functions in ramda

Sorted by popularity

function icon

ramda.clone is the most popular function in ramda (30311 examples)