我正在使用 jQuery 的 $.each()
方法来循环几个对象。有没有办法找出 $.each()
方法中是否有更多对象?
类似于:
$.each(data, function(o, myObject) {
if(data.hasMoreObjects) {
// append something
} else {
// append nothing
}
});
最佳答案
传递给回调函数的第一个参数是索引
。您可以将其与长度
进行比较,以确定是否有更多元素。
$.each(data, function(index, myObject) {
if (index === (data.length - 1)) {
// this is the last
} else {
// there are more coming
}
});
根据 data
的内容,您可能需要使用其他一些属性来代替 data.length
。例如,如果 data
是一个对象,您将使用 Object.keys(data).length
,
关于javascript - 检查 $.each() 中是否还有更多元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14201351/