亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 push() 方法和 indexOf() 方法從 AngularJS1 中的數組中刪除

使用 push() 方法和 indexOf() 方法從 AngularJS1 中的數組中刪除

慕碼人2483693 2023-04-27 15:00:59
我能夠提取 EPOCH 日期并將它們成功轉換為字符串,但是我之前編寫的代碼沒有檢查或刪除重復項。任何人都知道我可以添加什么來做到這一點?在這種情況下,時間戳是我從實時數據中提取的 EPOCH 日期!角度JS1
查看完整描述

3 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

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)


查看完整回答
反對 回復 2023-04-27
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

您可以使用set來消除重復:


const mySet= new Set(["one", "two", "three", "one"]);


mySet.has("one") // true            

mySet.size === 3); // true

Set 構造函數可以采用一組用于初始化集合的項目。


查看完整回答
反對 回復 2023-04-27
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

要檢查并刪除重復項,您可以使用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;


查看完整回答
反對 回復 2023-04-27
  • 3 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號