0

How should i access URL json array data using angular.forEace. i am trying to get data but giving some error please check below code i have added

[{
   "flightsDepartur":[
        {
          "fare":"9,000",
          "route":[
            {
              "source":"Delhi",
            }
             ],
         },
     ]
  }]

        $http.get('data.json').then(function(response) {
            $scope.allData = response;
         },function(dataStatus){
             console.log(dataStatus);
         });

    angular.forEach($scope.allData, function(flidata) {
        angular.forEach(flidata[0].flightsDepartur, function(flidatIn) {
            console.log(flidatIn.fare);
        });
    });

3 Answers 3

1

check the below to get fare and route data

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

app.controller('MainCtrl', function($scope) {
  $scope.name = [{
   "flightsDepartur":[
        {
          "fare":"9,000",
          "route":[
            {
              "source":"Delhi",
            }
             ],
         },
     ]
  }];
  angular.forEach($scope.name, function(k,v) {
        angular.forEach(k, function(k,v) {
       console.log(k[0].fare);
       console.log(k[0].route[0].source)
    });
    });
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
  
  </body>

</html>

Sign up to request clarification or add additional context in comments.

4 Comments

i have list of data i want to get data using forEach
please provide list
Every time i have to add k[0], this is not good "route":[ { "source":"Delhi", "destination":"Pune" } ], "timing":[ { "depart":"10:00 AM", "arrival":"12:00 AM" } ],
see in question you asked for console.log(flidatIn.fare); only...later added "how should i access my 'route' data".....and nnow your asking something else...so i suggesst you to please provide all your requirements at once and please provide the sufficient data acc to your requirement
1

The problem is you are assigning values to $scope.allData inside a promise. since the javascript asynchronous before the data being assign to $scope.allData for each loop start to execute. at that time $scope.allData was undefined. so it produces an error.

create a function and call that inside the promise to prevent the error

$http.get('data.json').then(function(response) {
      $scope.allData = response.data;
      callLoop()
 },function(dataStatus){
      console.log(dataStatus);
 });

function callLoop(){
    angular.forEach($scope.allData, function(flidata) {
        angular.forEach(flidata.flightsDepartur, function(flidatIn) {
            console.log(flidatIn.fare);
        });
    });
}

Comments

0

Try change

angular.forEach(flidata[0].flightsDepartur, function(flidatIn) {..

to

angular.forEach(flidata.flightsDepartur, function(flidatIn) {..

and when using then function to get response change to this.

 $http.get('data.json').then(function(response) {
        $scope.allData = response.data;
     },function(dataStatus){
         console.log(dataStatus);
     });

Demo

var app = angular.module('anApp', []);
app.controller('ctrl', function($scope) {
  $scope.allData = [{
    "flightsDepartur": [{
      "fare": "9,000",
      "route": [{
        "source": "Delhi",
      }],
    }, ]
  }]

  angular.forEach($scope.allData, function(flidata) {
    angular.forEach(flidata.flightsDepartur, function(flidatIn) {
      console.log(flidatIn.fare);
      console.log(flidatIn.route[0].source);
    });
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>

<div ng-app="anApp" ng-controller="ctrl">

</div>

3 Comments

i have change but how should i access my 'route' data
by flidatIn.route or flidatIn.route[0].source
I am using this but not getting anything angular.forEach($scope.allData, function(flidata) { //console.log(flidata); angular.forEach(flidata.flightsDepartur, function(flidatIn) { console.log(flidatIn); }); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.