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

前端 未结 5 663
Happy的楠姐
Happy的楠姐 2020-12-31 08:17

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

[
  {
    \"title\": \"EPAM\",
    \"technologies\": [
        \"PHP\",
        \".net\",
          


        
5条回答
  •  失恋的感觉
    2020-12-31 08:54

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

提交回复
热议问题