How to set object property (of object property of..) given its string name in JavaScript?

后端 未结 14 2053
离开以前
离开以前 2020-11-22 02:20

Suppose we are only given

var obj = {};
var propName = \"foo.bar.foobar\";

How can we set the prop

14条回答
  •  执念已碎
    2020-11-22 03:15

    I know it's an old one, but I see only custom functions in answers.
    If you don't mind using a library, look at lodash _.set and _.get function.

提交回复
热议问题