Convert JavaScript string in dot notation into an object reference

前端 未结 27 3017
梦如初夏
梦如初夏 2020-11-21 05:09

Given a JS object

var obj = { a: { b: \'1\', c: \'2\' } }

and a string

\"a.b\"

how can I convert the stri

27条回答
  •  悲&欢浪女
    2020-11-21 05:50

    If you can use lodash, there is a function, which does exactly that:

    _.get(object, path, [defaultValue])

    var val = _.get(obj, "a.b");
    

提交回复
热议问题