Answers for "get nested objects via string"

0

get nested objects via string

const get = require('lodash.get');

const myObj = { 
    user: { 
        firstName: 'Stacky', 
        lastName: 'Overflowy',
        list: ['zero', 'one', 'two']
    }, 
    id: 123 
};

console.log(get(myObj, 'user.firstName')); // outputs Stacky
console.log(get(myObj, 'id'));             // outputs 123
console.log(get(myObj, 'user.list[1]'));   // outputs one

// You can also update values
get(myObj, 'user').firstName = 'John';
Posted by: Guest on July-25-2021

Code answers related to "get nested objects via string"

Code answers related to "Javascript"

Browse Popular Code Answers by Language