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

Find the id of the object with the higher item, but below a limit

本文关键字:于限制 对象 ID 高项 查找      更新时间:2023-09-26

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

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

计划是一个日期,我做了一个函数来以 Date 对象的形式获取它。

现在,我需要在 smil.playlist 中找到过去的对象的 id(不如新的 Date 对象(并且更接近它。

在以前的版本中,我使用过滤器、排序和弹出来获取对象,但随着应用程序结构的变化,我现在需要它的 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);