javascript - JSON - How to reference using a wildcard? - Stack Overflow

I have a large JSON object where I can access the the value I want like so:$scope.customers[1][data][&q

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
Add a ment  | 

1 Answer 1

Reset to default 3

You 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

相关推荐

  • javascript - JSON - How to reference using a wildcard? - Stack Overflow

    I have a large JSON object where I can access the the value I want like so:$scope.customers[1][data][&q

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信