admin管理员组文章数量:1400652
I have a large JSON object where I can access the the value I want like so:
$scope.customers[1][data]["DisplayName"];
However, that references the DisplayName
of only one object.
Is there an easy way to return all DisplayName
properties from the entire collection?
i.e.
$scope.customers[*][data]["DisplayName"];
Or is the only way to do this by creating a new JSON object by looping through the original?
I have a large JSON object where I can access the the value I want like so:
$scope.customers[1][data]["DisplayName"];
However, that references the DisplayName
of only one object.
Is there an easy way to return all DisplayName
properties from the entire collection?
i.e.
$scope.customers[*][data]["DisplayName"];
Or is the only way to do this by creating a new JSON object by looping through the original?
Share Improve this question asked Dec 8, 2014 at 17:55 Raphael RafatpanahRaphael Rafatpanah 20.1k28 gold badges105 silver badges174 bronze badges 4- 1 Looping/filtering is the only way. You may find helper methods or modules that simplify it, but in the end they're all going to loop over it. – Kevin B Commented Dec 8, 2014 at 17:57
- Is there a filter available in Angular that can do this? – Raphael Rafatpanah Commented Dec 8, 2014 at 18:00
- Yeah, `ng-repeat will do that for you. – Zack Huber Commented Dec 8, 2014 at 18:01
-
3
@RaphaelRafatpanah
[].map
would likely be easier to implement than any filter angular has.var displayNames = $scope.customers.map(function (d) {return d[data]["DisplayName"];});
– Kevin B Commented Dec 8, 2014 at 18:03
1 Answer
Reset to default 3You can use map() function of Array. It applies the callback function to each element of array and produces new array:
[1,2,3].map(function(num) { return num * num }); // returns [1, 4, 9]
In your case you can use:
$scope.customers.map(function(element) { return element[data]["DisplayName"]; } );
本文标签: javascriptJSONHow to reference using a wildcardStack Overflow
版权声明:本文标题:javascript - JSON - How to reference using a wildcard? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744267001a2597995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论