Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through nested json array in angular controller and get unique values

I need to iterate through nested json array which looks like that

[
  {
    "title": "EPAM",
    "technologies": [
        "PHP",
        ".net",
        "Java",
        "Mobile",
        "Objective-C",
        "Python",
        "Ruby"
    ],
    "location": "Belarus",
    "city": "Minsk" 
  },
  {
    "title": "Parallels",
    "technologies": [
        "PHP",
        "Java",
        "C++",
        "iOS Development",
        "C#",
        "Ember.js"
    ],
    "location": "Russia",
    "city": "Moscow" 
  }
]

What I want is to iterate through list of technologies in each company and then return a list of unique values. I failed, however, to access a single company in company arrays in the controller. It looks like this so far

var app = angular.module('app', []);

app.controller('CompaniesController', ['$scope', '$http', 
  function($scope, $http) {
    $http.get('json/companies.json').success(function(data) {
        $scope.companies = data; // get data from json

        $scope.techStack = []

        $scope.companies = data.query(function(companies) {
            console.log(companies); //I expected to see data here
        });
    });

  }
]); 

Apparently I'm doing something wrong.

like image 239
vitalym Avatar asked Oct 01 '14 10:10

vitalym


1 Answers

Use angular's forEach:

 var app = angular.module('app', []);
    app.controller('CompaniesController', ['$scope', '$http', 
      function($scope, $http) {
        $http.get('json/companies.json').success(function(data) {
            $scope.companies = data; // get data from json
              angular.forEach($scope.companies, function(item){
                   console.log(item.technologies);  
               })
            });
        });

      }
    ]); 
like image 166
user3681587 Avatar answered Oct 19 '22 03:10

user3681587