2016-01-25 90 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