2016-09-29 108 views
-1

我有一個對象像下面我想刪除所有鍵值其中key =「/」下劃線過濾對象

let routes = { 
    '/dashboard': { 
     name : 'Dashboard', 
     component : appDashboard, 
     icon: 'fa fa-dashboard', 
     subRoutes: { 
      '/': { 

       component:appDashEcommerce 
      }, 
      '/ecommerce': { 
       name : 'Ecommerce', 
       component:appDashEcommerce 
      }, 

     } 

    }, 
    '/apps': { 
     name : 'Apps', 
     component : appAppsPage, 
     icon : 'fa fa-th', 
     subRoutes: { 
      '/': { 

       component:appInbox 
      }, 
      '/mailbox': { 
       name : 'maibox', 
       component : appInbox, 
       icon : 'fa fa-th', 
      } 
     } 
}; 

我當前的代碼

var ret2 = _.omit(routes, function(val, key, object) { 
     if(_.has(val , 'subRoutes')){ 
      _.omit(val.subRoutes , function(v, k, o) { 
       return key === '/' 
      }) 
      }else{ 

      return key === '/' || key === '*' 
     } 
     }) 

    console.log(ret2) 
+2

難道你有問題還是你剛剛說了你的胸部。 – 2016-09-29 21:47:52

+0

對不起,這是一個問題,你有答案嗎? –

+1

我有一個,但它更簡單,因爲它不使用整個庫來實現簡單的事情 - > https://jsfiddle.net/qspunug6/1/ – adeneo

回答

1

我相信你使用你的內部函數的錯誤變量。

編輯:對不起,我懶惰,試圖寫沒有測試。上述論斷是正確的,但也有一些其他錯誤以及,解決在更新的代碼如下

https://jsfiddle.net/e708gyna/

//_.omitBy should be used for functions according to the lodash spec 
var ret2 = _.omitBy(routes, function(val, key, object) { 
    if(_.has(val , 'subRoutes')) { 
    //In order to use the result below, we need to store it 
    val.subRoutes = _.omitBy(val.subRoutes , function(v, k, o) { 
     //Since you're running this on the subRoutes 
     //you need to test the key that you've defined 
     //for this inner handler 
     return (k === '/'); 
    }) 
    } else { 
    return (key === '/' || key === '*'); 
    } 
}) 
+0

不幸的是,這似乎並沒有工作 –

+1

固定。在第一次運行中我還沒有修復其他一些錯誤。 –

+0

@ dominic-aquiliana這修復它謝謝 –