2017-08-29 6 views
1

私はVueJS、LodashとBabelでプロジェクトをやっています。私の問題は、routes.jsファイルからのルートで私のアプリケーションのサイドバーに値を設定する必要があるということです。問題は、私が必要とするデータをどのように取得できるのか本当に分かりません。ES6ネストされたオブジェクトからプロパティを取得

私のroutesファイルである
export default [ 
    { 
    path: "/admin/login", 
    name: "admin.login", 
    component: Login 
    }, 
    { 
    path: "/admin", 
    component: MasterLayout, 
    meta: { auth: true }, 
    children: [ 
     { 
     path: "/admin/dashboard", 
     alias: "/", 
     menu: { text: "", icon: ""}, 
     name: "admin.dashboard", 
     component: DashboardIndex 
     } 
    ] 
    }, 
    { 
    path: '/403', 
    name: 'error.forbidden', 
    component: ErrorForbidden 
    }, 
    { 
    path: "*", 
    name: 'error.notfound', 
    component: ErrorPageNotFound 
    } 
]; 

、私は基本的に各ルートを反復処理する必要があり、それは子供またはメニュー性質を持っているかどうかを確認、それがメニューを持っている場合、それがサイドバーの私のリストに追加されますアイテム、それの子供たちが自分のメニューと同様に最終的には

を引き抜かれるプロパティを追加されます私は、次の

のようなものを取得したいのですが

これは、メニュープロパティを含むルートのみを考慮する必要があります。

+0

を持っていますか? 'name: 'Dashboard'と名前: 'admin.dashboard'例えば –

+0

@KoushikChatterjee彼らは異なったオブジェクトです、最初は生の' routes.js'オブジェクトです。もう一つは実行時にその初期オブジェクトから生成されたものです。 –

答えて

0
あなたは基本的には、アレイ上の再帰反復必要

ので、これを試してください:ここでは

function iterateArrayRoutes(routeArray) { 
    if(routeArray instanceof Array) { 
    var routeFormatted = routeArray.reduce(function(last, route){ 
     if (route.hasOwnProperty("menu")) { 
     var item = { 
      name: route.name, 
      icon: route.menu.icon 
     }; 
     if(route.hasOwnProperty("children") && route.children.length > 0) { 
      item.children = iterateArrayRoutes(route.children); 
     } 
     last.push(item); 
     } 
     return last; 
    }, []); 
    return routeFormatted; 
    } 
}; 

あなたは同じオブジェクトが2 `name`の性質を持つことができますどのようにデモhttps://jsfiddle.net/vm1hrwLL/

関連する問題