VolcovMeter VolcovMeter - 1 year ago 52
JSON Question

AngularJS: How to create new arrays from JSON response?

I have a function tied to the factory for my controller:

fac.GetDailyCountersList = function () {
return $http.get('/Data/GetDailyCountersList')

Inside the controller, here's where the function is called:

$scope.DailyCounters = null;
DailyCounterService.GetDailyCountersList().then(function (d) {
$scope.DailyCounters = d.data;
}, function (error) {

Based on this, if I were to output the variable on the html page, this would show:


My question is how to take this data and create other arrays based on one of the properties of the Json data.

For example, how would I put arrays


Into a new array "ArrayDay16"? And the rest into their respective arrays as well.

I was thinking about doing it this way:

var myArray = [];
if (d.data['MIDay'] == '16')

But this is incorrect/fails. Any ideas for an efficient solution?

Answer Source

Use the filter function to populate new arrays. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

//This will populate "someArray" with only the data that has MIDAY = 16
DailyCounterService.GetDailyCountersList().then(function (d) {
    $scope.someArray = d.data.filter(function(ele) {
        return ele.MIDAY === '16';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download