var keys = ['name','age','gender'];
var input = {"document":
{"people":[
{"name":["Harry Potter"],"age":["18"],"gender":["Male"]},
{"name":["hermione granger"],"age":["18"],"gender":["Female"]},
]}
}
如何从特定密钥获取值。示例{Harry Potter, 18, Male}
?
答案 0 :(得分:1)
您可以使用 pick 来提取您感兴趣的属性:
var people = _.map(input.document.people, function(person){
return _.flatten( _.values(_.pick(person, keys)));
});
angular.module('MyModule', [])
.controller('MyController', function($scope) {
var keys = ['name','age','gender'];
var input = {"document":
{"people":[
{"name":["Harry Potter"],"age":["18"],"gender":["Male"]},
{"name":["hermione granger"],"age":["18"],"gender":["Female"]}
]}
};
var people = _.map(input.document.people, function(person){
return _.flatten( _.values(_.pick(person, keys)));
});
$scope.people = people;
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='MyModule' ng-controller='MyController'>
<p ng-repeat='person in people'>{{person | json}}</p>
</div>
答案 1 :(得分:-1)
通过 input.document.people 进行迭代,并使用keys数组中的键作为
for(x in input.document.people){
for(y in keys){
console.log(input.document.people[x][keys[y]])
}
}
请refer to fiddle获取相同的