1
我遇到了麻烦数组中的数组是空的。如何用jQuery .each()处理空对象?
我得到两个数组,我推入一个新的,所以我可以使用jQuery .each()与新的数组。新的阵列可以具有不同的状态:
包含空数组:
nextEvent = [[ ], [ ]]
含有包含每一个对象两个完整的阵列:
nextEvent = [[object], [object]]
或含有一个完整的阵列与一个对象:
nextEvent = [[object], [ ]]
nextEvent = [[ ], [object]]
现在我试图使用.each()在nextEvent
,或将现有对象数据追加到元素。
到目前为止,我得到了一个函数,它看起来像这样:
$.each(nextEvent, function(i, n){
if (nextFeedingTime || nextShowTime){
var time = n[0].Time.toString();
//...
if (nextFeedingTime && nextShowTime){
if (n[0].Kategorie === "Fütterung"){
appendFeeding();
} else {
appendShow();
}
} else if (nextFeedingTime && !nextShowTime){
appendFeeding();
} else if (!nextFeedingTime && nextShowTime){
appendShow();
}
} else {
//...
}
});
注:nextFeedingTime
和nextShowTime
是我最初推入nextEvent
此功能工作的阵列,但只要一个数组是空的我得到一个n[0] is undefined
错误。任何想法如何解决这一问题?对于不确定的检查
if(n[0] !== undefined)
检查,并着手:
然后该函数在'nextEvent = [[],[object]]'的情况下不起作用,因为没有定义第一个'n [0]'。我仍然需要它来使用第二个对象。 –
@kvothe将通过'$ .each'的下一次迭代来处理。 –
@KevinB你是对的,在我的代码的其他地方发现了错误。 @ jp310谢谢,我加了'if(n [0])'这可能是最简单的方法 –