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"]; } );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744267001a4565913.html
评论列表(0条)