Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用push()方法和indexOf()方法从AngularJS1中的数组中删除/检查重复日期_Javascript_Angularjs_Duplicates_Indexof_Epoch - Fatal编程技术网

Javascript 使用push()方法和indexOf()方法从AngularJS1中的数组中删除/检查重复日期

Javascript 使用push()方法和indexOf()方法从AngularJS1中的数组中删除/检查重复日期,javascript,angularjs,duplicates,indexof,epoch,Javascript,Angularjs,Duplicates,Indexof,Epoch,我能够成功地提取历元日期并将其转换为字符串,但是我之前编写的代码没有检查或删除重复项。有人知道我可以添加什么来实现这一点吗?在本例中,时间戳是我从实时数据中提取的纪元日期!ANGULARJS1您可以使用set消除重复: var dateMap = {}; for (i = 0; i < dbData.length; i++) { let tmp_date_str = ""; let tmp_date = new Date(dbData[i].TIMESTAMP)

我能够成功地提取历元日期并将其转换为字符串,但是我之前编写的代码没有检查或删除重复项。有人知道我可以添加什么来实现这一点吗?在本例中,时间戳是我从实时数据中提取的纪元日期!ANGULARJS1

您可以使用
set
消除重复:

var dateMap = {};
for (i = 0; i < dbData.length; i++) {
  let tmp_date_str = "";
  let tmp_date = new Date(dbData[i].TIMESTAMP);

  tmp_date_str += tmp_date.getFullYear();
  tmp_date_str += "-";

  if ((tmp_date.getMonth()+1) < 10) {
      tmp_date_str += "0";
  }

  tmp_date_str += (tmp_date.getMonth()+1);
  tmp_date_str += "-";

  if (tmp_date.getDate() < 10) {
      tmp_date_str += "0";
  }
  tmp_date_str += tmp_date.getDate();

    // make it a map, and if value for this string already exists, do nothing
  if (!dateMap[tmp_date_str]) {
    dateMap[tmp_date_str] = true;
  }
}
const mySet= new Set(["one", "two", "three", "one"]);

mySet.has("one") // true            
mySet.size === 3); // true
集合构造函数可以接受
用于初始化集合的项目。

要检查和删除重复项,可以使用
角度。等于

var newArray = [];
            angular.forEach($scope.dates, function(value, key) {
                var exists = false;

                angular.forEach(newArray, function(val2, key) {
                if(angular.equals(value.date, val2.date)){ exists = true }; 
                });

                if(exists == false && value.dates != "") { newArray.push(value); }
            });

            $scope.dates = newArray;
var newArray = [];
            angular.forEach($scope.dates, function(value, key) {
                var exists = false;

                angular.forEach(newArray, function(val2, key) {
                if(angular.equals(value.date, val2.date)){ exists = true }; 
                });

                if(exists == false && value.dates != "") { newArray.push(value); }
            });

            $scope.dates = newArray;