What is the best way to get/set an objects deep property in Javascript?

前端 未结 2 518
孤街浪徒
孤街浪徒 2021-01-24 18:00

It is notoriously annoying to get a deeper property from an object. If the parent does not exist, you get an error. So you need to stop getting to the property when a parent doe

2条回答
  •  醉梦人生
    2021-01-24 18:31

    Instead of try..catch, just test for the property:

    function getProp(path, parent) {
        path = path.split('.');
    
        for (var k=0, kLen=path.length; k

提交回复
热议问题