簡體   English   中英

用jquery。()。each和for循環迭代失敗

[英]Iterating with a jquery.().each and for loop fails

我試圖從兩個對象中獲取數據,並避免(this)關鍵字發生沖突,我認為使用與兩個對象的each和for循環會起作用。 但是我不能讓他們適當合作。 令人沮喪的是,我現在在控制台中出現錯誤。 我的代碼:

var times = {
  startTimes: [{
    start: '10:00'
  }, {
    start: '11:00'
  }, {
    start: '12:00'
  }],
  endTimes: [{
    end: '10:00'
  }, {
    end: '11:00'
  }, {
    end: '12:00'
  }]
}
var items = [];
jQuery(obj).each(function() {
  if (this.ID === parseInt(inputID)) {

    for (var i in times) {
      var item = {
        id: this.ID,
        start: times.startTimes[i].start,
        end: times.endTimes[i].end
      }
      items.push(item);
    }

  }
});

我做錯了什么? 有什么建議么?

.each()是jQuery對象的功能,來自官方文檔,位於: .each()

描述:遍歷jQuery對象,為每個匹配的元素執行一個函數。

和:

調用時,它將遍歷作為jQuery對象一部分的DOM元素。

而且您在肯定不是由DOM對象組成的對象上調用它。

您可以使用評論中建議的內容或在此處選擇香草:

    arr.forEach(function callback(currentValue, index, array) {
       //your iterator
    }[, thisArg]);

請參閱文檔: forEach()

另外,內部邏輯對我來說很奇怪,您在這里想要實現什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM