0

I have a JSON that is returning Objects inside of an array.

I'm using angular.forEach to iterate the data. Each object has a date to which I would like to compare with the next object in the list.

How could this be done?

The snippet below is returning all the dates, I would like to check if the next entry is the same date as the previous. If it is not then compare the two values with Moment.js

So the first [0] and second [1] have the same date, however the third date [2] has "2015-01-03" I need to catch this and compare the two values.

Your help is greatly appreciated!

    $scope.my_data = [
      {
        date: "2015-01-02"
      },
      {
        date: "2015-01-02"
      },
      {
        date: "2015-01-03"
      },
    ];

    angular.forEach($scope.my_data, function(value1, key1) {
      var missing_days = value1.date;
      console.log('missing days', missing_days);
    });

1 Answer 1

1

You can easily do this with angular.forEach, just use the provided index to look ahead one place in the original data set

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

app.controller('MainCtrl', function($scope) {
  $scope.my_data = [{
    date: "2015-01-02"
  }, {
    date: "2015-01-02"
  }, {
    date: "2015-01-03"
  }, ];

  angular.forEach($scope.my_data, function(value, key) {
      var missing_days = value.date;
      console.log('missing days', missing_days);
      var nextObject = $scope.my_data[key+1 % $scope.my_data.length];
      console.log(nextObject);
  });
});

http://plnkr.co/edit/y4qeNe?p=preview

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

Comments

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.