Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
To be honest you're asking one question initially, and then what you show in your controller is not nearly relevant to the first part of your question. However I think you can get at what you want by a couple of for loops and a helper method. – Mohammad Sepahvand Oct 1 '14 at 10:44
up vote 18 down vote accepted

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);  
               })
            });
        });

      }
    ]); 
share|improve this answer

In order to loop through array in AngularJS, you can simply use angular.forEach. For example,

angular.forEach(companiesList, function(company) {
    //Here you can access each company.
});

I have made a simple demo based on your code that list "Companies" and unique "Technologies".

DEMO

share|improve this answer

If you need only to display nested array on UI, you can do that straight in view e.g.

            <tr ng-repeat="i in Items">
                <td valign="top">{{i.Value}}</td>
                <td valign="top">
                    <table>
                        <tr ng-repeat="c in i.Children">
                            <td>{{c.Value}}</td>
                        </tr>
                    </table>
                </td>
            </tr>
share|improve this answer

try this

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 = []

        angular.forEach($scope.companies, function(item){
            $scope.techStack = item.technologies;
            var uniqueTech = [];
            for (var i = 0; i < $scope.techStack.length; i++)
            {
                if (uniqueTech.indexOf($scope.techStack[i]) == -1)
                    uniqueTech.push($scope.techStack[i]);
            }   
            console.log("Unique Technologies : " + uniqueTech);

        })
    });

  }
]); 
share|improve this answer

user angular's foreach function for looping through data.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.