查找具有较高项目但低于限制的对象的ID

我有一个JavaScript对象的数组,对象是这样的:

smil .____.update (object) | |... | |____.stream (array) | |... | |____.playlist (array of object) |____.name (string) |____.scheduled |____... 

Scheduled是一个date,我已经做了一个函数来获取它的Date对象的forms。

现在,我需要find过去的smil.playlist中的对象的id(比新的Date对象差),并且更接近它。

在之前的版本中,我使用filter,sorting和popup来获取对象,但是随着应用程序结构的变化,我现在需要它的ID。

旧代码:

 function get_last_playlist(smilp){ //get current date time = new Date(); //Delete all of playlist which are in the future smilp.filter(function(element) { return time.getTime() > gettime(element.scheduled).getTime(); }); //Sort by close to current time (higher scheduled) smilp.sort(function(a, b) { return gettime(a.scheduled) - gettime(b.scheduled); }); //return the first in the array return smil.pop(); } 

有没有办法在smilp.playlist中返回对象的ID,还是我需要重新编写大部分的应用程序?

你可以这样做:

 function getclosest(closest, element, index, array){ if((time.getTime()-gettime(element.scheduled).getTime()) < (time.getTime()-gettime(closest.scheduled).getTime()) ) return index; else return closest; } smil.reduce(getclosest);