Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON - How to reference using a wildcard?

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?

like image 225
Raphael Rafatpanah Avatar asked Jun 03 '26 23:06

Raphael Rafatpanah


1 Answers

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"]; } );
like image 183
ceth Avatar answered Jun 06 '26 13:06

ceth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!