我正在尝试从两个对象获取数据,并避免 (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()
Description: Iterate over a jQuery object, executing a function for each matched element.
还有:
When called it iterates over the DOM elements that are part of the jQuery object.
并且您在肯定不是由 DOM 对象组成的东西上调用它。
您可以使用评论中建议的内容或在此处选择普通:
arr.forEach(function callback(currentValue, index, array) {
//your iterator
}[, thisArg]);
请参阅文档:forEach()
另外,内部逻辑对我来说似乎很奇怪,你想在这里实现什么目标?
关于javascript - 使用 jquery.().each 和 for 循环进行迭代失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47696878/