showing results for - "get nested objects via string"
Adelia
14 Sep 2019
1const get = require('lodash.get');
2
3const myObj = { 
4    user: { 
5        firstName: 'Stacky', 
6        lastName: 'Overflowy',
7        list: ['zero', 'one', 'two']
8    }, 
9    id: 123 
10};
11
12console.log(get(myObj, 'user.firstName')); // outputs Stacky
13console.log(get(myObj, 'id'));             // outputs 123
14console.log(get(myObj, 'user.list[1]'));   // outputs one
15
16// You can also update values
17get(myObj, 'user').firstName = 'John';
18