-1

I have a Json objects, which contains one field with date, I need to take it and prepare for other processing, but I don't know how to take just one field from one object, I tried to do that:

app.controller('timerCtrl', function ($scope) {
   var endDate =  $scope.data.list[1].end;// but list[1] prints just list[1].ends value, but how can i get and prepare the fields of other objects?
});

Json-object:

[
{
    "id": "1",
    "name": "New shop",
    "shop": "DNS",
    "begin": "09-05-2017",
    "end":" 12-05-2017"

},
{
    "id": "2",
    "name": "New shop",
    "shop": "M-video",
    "begin":"05-05-2017",
    "end":"15-05-2017"
}

]

Will
  • 6,601
  • 3
  • 31
  • 42
IvanNik
  • 11
  • 3
  • 2
    Possible duplicate of [Loop through an array in JavaScript](http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Igor May 11 '17 at 20:20

2 Answers2

0

You could loop through the list like this

for (var i = 0; i < $scope.data.list.length; i++){
    var endDate =  $scope.data.list[i].end;
}      

Alternatively, you could get an array of the dates by maping them to another array like this. Then, you could pass that on for further processing, depending on what you're doing.

 var endDates = $scope.data.list.map(function(listItem) { return listItem.end; });
Nicholas J. Markkula
  • 1,542
  • 4
  • 18
  • 30
0

I guess this question will probably close, but here's the 'angular iterator' way to do it as you asked:

https://docs.angularjs.org/api/ng/function/angular.forEach

Enjoy bombarding your stack if you have a large json.

Or you can always treat it like just another array and use a for loop.

Devesh Sati
  • 666
  • 4
  • 15