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



我能够成功地提取EPOCH日期并将其转换为字符串,但我之前编码的内容没有检查或删除重复项。有人知道我可以添加什么吗?在这种情况下,时间戳是我从实时数据中提取的EPOCH日期!ANGULARJS1

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;
}
}

如果你需要将日期作为一个数组,只需进行

var dates = Object.keys(dateMap)

您可以使用set来消除重复:

const mySet= new Set(["one", "two", "three", "one"]);
mySet.has("one") // true            
mySet.size === 3); // true

Set构造函数可以接受用于初始化集合的项。

对于检查和删除重复项,可以使用angular.equals:

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;

最新更新