shreya gupta
shreya gupta

Reputation: 141

duplicate values are showing

I have a dropdown in which I am showing the dates:-

<label>Date:</label>
   <select data-ng-model="date" name="viewDate" data-ng-options="d for d in newDates" data-ng-change="selectDate(date)" required="required">
        <option value="">Please Select</option>
   </select>

Directive Code:-

scope.getDates = function () {
     scope.newDates = [];
    ApiServices.getAllDates().then(
        function (response) {
          scope.dates = response.data;
              scope.dates.forEach(function (entry,index) {    
                  entry = moment(entry).format('YYYY-MM-DD');
                  scope.newDates.push(entry);
        });
     if (scope.dates.length) {
           scope.noRecords = false;
    } else {
          scope.noRecords = true;
     }
  },
   function (err) {
      // Handle error here
    console.log('Error' + JSON.stringify(err.data));
     });
  };

As date is coming in epoch I am trying to convert it in the human readable form. But the values are showing duplicate for each entry. I think the code I wrote for converting epoch ino string is wrong.Can anyone tell me my mistake.

Upvotes: 2

Views: 72

Answers (3)

Umair Sarfraz
Umair Sarfraz

Reputation: 5953

scope.getDates = function () {
  $scope.humanizedDates = [];
  ApiServices.getAllDates().then(
    function (response) {
      if (response.data.length) {
        $scope.humanizedDates = response.data.map(function(date){
          return moment(date).format('YYYY-MM-DD');
        });
        scope.noRecords = false;
      } else {
        scope.noRecords = true;
      }
    },
    function (err) {
      // Handle error here
      console.log('Error' + JSON.stringify(err.data));
    });
};

Upvotes: 0

shreya gupta
shreya gupta

Reputation: 141

Here the solution I tried and its working.No duplicate values now.

    scope.getDates = function () {
         scope.newDates = new Set();
        ApiServices.getAllDates().then(
            function (response) {
              scope.dates = response.data;
                 scope.dates.forEach(function (entry,index) { 
                    scope.newDates = scope.dates.map(function(entry){
                        entry = moment(entry).format('YYYY-MM-DD');
                             return entry;
                     });
                });
         if (scope.dates.length) {
               scope.noRecords = false;
        } else {
              scope.noRecords = true;
         }
      },
       function (err) {
          // Handle error here
        console.log('Error' + JSON.stringify(err.data));
         });
      };

Upvotes: 2

developer_hatch
developer_hatch

Reputation: 16224

If you don't want duplicated values, you can create a set instead a list.

scope.getDates = function () {
     scope.newDates = new Set();
    ApiServices.getAllDates().then(
        function (response) {
          scope.dates = response.data;
              scope.dates.forEach(function (entry,index) {    
                  entry = moment(entry, 'YYYY-MM-DD').format();
                  scope.newDates.add(entry);
        });
     if (scope.dates.length) {
           scope.noRecords = false;
    } else {
          scope.noRecords = true;
     }
  },
   function (err) {
      // Handle error here
    console.log('Error' + JSON.stringify(err.data));
     });
  };

Upvotes: 1

Related Questions