Set object values with a path string in lodash with _.set

A few months ago I wrote a post on the _.get method so for todays post on lodash I might as well write one on _.set as well. The _.set method works just like that of the _.get method in lodash, only it can be used to set a property rather than getting it.

1 - Basic _.set example

The basic idea of _.set is that a value can be set by just passing the object, and then a path in string format, followed by the value to set the property to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let enemy = {
// enemy health
health: {
current: 80,
max: 100,
healRate: {
active: false,
perTick: 5
}
},
// index values of targets
targets: [2, 6, 8]
};
// a path
let path = 'health.healRate.active';
// set the value at path
_.set(enemy, path, true);
// get the value at path
console.log( _.get(enemy, path) ); // true

2 - _.set creates a path if it is not there

It’s not like setting a property of an object is all that hard without lodash, but this method allows for setting the value with a string format path which can be helpful in some situations. Another added benefit is that it can also be used to create paths in the event that they are not there as well.

1
2
3
4
5
6
7
8
9
let _ = require('lodash');
let foo = {};
let path = 'bar.foobar.answer.to.life';
_.set(foo, path, 42);
console.log(_.get(foo,path)); // 42