2016-01-25 91 views
0

我需要創建一種哈希表,我無法做到檢索某些值。這是情況;獲取將值傳遞給javascript的值

我有一個JSON有像這樣的一些屬性:

$scope.getItems = { 
     "data": [{ 
      "label": "first-label", 
       "objects": [{ 
       "name": "firstObj", 
        "attributes": [{ 
        "attrname": "Item1", 
         "attrValue": "", 
         "attrType": "text" 
       }, { 
        "attrname": "Item2", 
         "attrValue": "", 
         "attrType": "text" 
       }] 
      }], 
       "properties": { 
       "film:property": "action" 
      } 
     }, 
     { 
      "label": "second-label", 
       "objects": [{ 
       "name": "firstObj", 
        "attributes": [{ 
        "attrname": "Item1", 
         "attrValue": "", 
         "attrType": "text" 
       }, { 
        "attrname": "Item2", 
         "attrValue": "", 
         "attrType": "text" 
       }] 
      }], 
       "properties": { 
       "film:property": "action" 
      } 
     }, 
     { 
      "label": "third-label", 
       "objects": [{ 
       "name": "firstObj", 
        "attributes": [{ 
        "attrname": "Item1", 
         "attrValue": "", 
         "attrType": "text" 
       }, { 
        "attrname": "Item2", 
         "attrValue": "", 
         "attrType": "text" 
       }] 
      }], 
       "properties": { 
       "film:property": "drama" 
      } 
     }, 
     { 
      "label": "fourth-label", 
       "objects": [{ 
       "name": "firstObj", 
        "attributes": [{ 
        "attrname": "Item1", 
         "attrValue": "", 
         "attrType": "text" 
       }, { 
        "attrname": "Item2", 
         "attrValue": "", 
         "attrType": "text" 
       }] 
      }], 
       "properties": { 
       "film:property": "action" 
      } 
     }, 
     { 
      "label": "fifth-label", 
       "objects": [{ 
       "name": "firstObj", 
        "attributes": [{ 
        "attrname": "Item1", 
         "attrValue": "", 
         "attrType": "text" 
       }, { 
        "attrname": "Item2", 
         "attrValue": "", 
         "attrType": "text" 
       }] 
      }], 
       "properties": { 
       "film:property": "comic" 
      } 
     } 

     ] 
    }; 

那好吧,現在,我需要的是film:propertylabel。 正如你所看到的電影:財產「行動」,我們有一個以上的標籤,exaclty:

- first-label 
- second-label 
- fourth-label 

這樣一個假設樹視圖可能是:

- action 
    - first-label 
    - second-label 
    - fourth-label 

-drama 
    - third-label 

-comic 
    -fifth-label 

我在這裏做了一個的jsfiddle: https://jsfiddle.net/vuqcopm7/58/其中有相同的JSON和像這樣的列表:

<ul data-ng-repeat="object in uniqueNames"> 
     <li ng-click="getLabelByProp(object)"> 
      <a style="cursor:pointer">{{object}}</a> 
     </li> 
     </ul> 

我需要的是當我點擊在項目與property.For例如獲得標籤:

在行動點擊我需要的地方顯示,它並不重要,因爲,

- first-label 
    - second-label 
    - fourth-label 

等爲他人。感謝幫助!

回答

1

我用過濾器和地圖來得到你的結果。 的匹配屬性,以便第一過濾器,然後創建一個數組只的標籤和最終得到的唯一值:

$scope.getLabelByProp = function(property) { 
    var filtered = $scope.getItems.data 
       .filter(byProperty(property)) 
       .map(getLabel) 
       .filter(isUnique) 
    console.log(filtered); 
} 

function isUnique(item, idx, items) { 
    return items.indexOf(item) === idx; 
} 

function byProperty(property) { 
    return function(item, i, items) { 
    return item.properties["film:property"] == property 
    } 
} 

function getLabel(n) { 
    return n.label; 
} 

我不那麼肯定的獨特性,雖然。

fiddle

+0

是!感謝的人似乎正是我期待的! –

1

我想這個數據映射到一個新的結構genrecategory爲頂層和個人電影兒童

[ 
    {genre: 'action', films:[]}, 
    {genre: 'drama', films:[]} 
] 

繼通過所有的電影圈,並使用genre關鍵將電影分組成各自的子陣列。

var tmp = {}; 

data.forEach(function(item) { 
    var genre = item.properties["film:property"]; 
    if (!tmp[genre]) { 
    tmp[genre] = [] 
    } 
    tmp[genre].push(item) 
}); 

一旦所有都在臨時對象是那些鍵的簡單地圖,以創建最終範圍模型

$scope.filmCats = Object.keys(tmp).map(function(key) { 
    return { 
    genre: key, 
    films: tmp[key] 
    } 
}); 

基本HTML

<div ng-repeat="item in filmCats"> 
    <h4>{{item.genre}}</h4> 
    <ul> 
     <li ng-repeat="film in item.films">{{film.label}} 
     <ul> 
      <li ng-repeat="obj in film.objects">{{obj.name}}</li> 
     </ul> 
     </li> 
    </ul> 
    </div> 

注意:您也可以做到這一點與過濾器groupBy,只需要添加一個新的屬性名稱,每個電影的值是"film:property"

DEMO