简体   繁体   English

JSON-如何使用通配符进行引用?

[英]JSON - How to reference using a wildcard?

I have a large JSON object where I can access the the value I want like so: 我有一个大的JSON对象,可以在其中访问所需的值,如下所示:

$scope.customers[1][data]["DisplayName"];

However, that references the DisplayName of only one object. 但是,它仅引用一个对象的DisplayName

Is there an easy way to return all DisplayName properties from the entire collection? 有没有一种简单的方法可以从整个集合中返回所有DisplayName属性?

ie

$scope.customers[*][data]["DisplayName"];

Or is the only way to do this by creating a new JSON object by looping through the original? 还是通过遍历原始对象创建新的JSON对象来做到这一点的唯一方法?

You can use map() function of Array. 您可以使用Array的map()函数。 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"]; } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM