Reputation: 7863
I have an array of javascript object, the object is as this:
smil
.____.update (object)
| |...
|
|____.stream (array)
| |...
|
|____.playlist (array of object)
|____.name (string)
|____.scheduled
|____...
Scheduled is a date, I have made a function to get it in the form of a Date object.
Now, I need to find the id of the object in smil.playlist which is in the past (inferior to a new Date object) and the closer to it.
In a previous version, I was using filter, sort and pop to get the object, but following change in the structure of the app, I now need the id of it.
old code:
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();
}
Is there a way to return the id of the object in smilp.playlist, or do I need to rewrite a large part of the app again?
Upvotes: 1
Views: 83
Reputation: 48003
You can do it like this :
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);
Upvotes: 1