Full path of a json object

前端 未结 9 803
忘了有多久
忘了有多久 2021-01-12 06:02

I\'m trying to flatten an object where the keys will be the full path to the leaf node. I can recursively identify which are the leaf nodes but stuck trying to construct the

9条回答
  •  长情又很酷
    2021-01-12 06:43

    I find a tiny JavaScript utility to access properties using path. It is called object-path and is an opensource project on GitHub.

    To get attribute from an object:

    objectPath.get(obj, "a.b");
    

    to set attribute:

    objectPath.set(obj, "a.b", value);
    

    to remove an attribute:

    objectPath.del(obj, "a.b");
    

    So easy!!

提交回复
热议问题