Find by key deep in a nested array

后端 未结 17 1595
礼貌的吻别
礼貌的吻别 2020-11-22 15:41

Let\'s say I have an object:

[
    {
        \'title\': \"some title\"
        \'channel_id\':\'123we\'
        \'options\': [
                    {
                 


        
17条回答
  •  面向向阳花
    2020-11-22 16:07

        fucntion getPath(obj, path, index = 0) {
            const nestedKeys = path.split('.')
            const selectedKey = nestedKeys[index]
    
            if (index === nestedKeys.length - 1) {
                return obj[selectedKey]
            }
    
            if (!obj.hasOwnProperty(selectedKey)) {
                return {}
            }
    
            const nextObj = obj[selectedKey]
    
            return Utils.hasPath(nextObj, path, index + 1)
        }

    You're welcome By: Gorillaz

提交回复
热议问题